In Swift, I can do this to define a variable:
let foo: String = {
if bar {
return "42"
} else {
return "43"
}
}()
How can I define a variable like this in JavaScript? I know that you can define a variable as undefined and redefine it in the if block, but that's an ugly syntax IMO, since "foo" would get repeated 3 times instead of 1 in the Swift example:
let foo
if (bar) {
foo = "42"
} else {
foo = "43"
}