Shouldn't it be like
Arguably, it should be:
this.askTeller = function(pass) {
if (pass == 1234) return bankBalance;
return "Wrong password.";
};
or
this.askTeller = function(pass) {
return pass == 1234 ? bankBalance : "Wrong password.";
};
E.g., there's no point to the else
at all.
But getting to your point about {}
: They're optional. Control-flow structures like if
(and while
, and for
, etc.) are connected to the one statement that follows them; if you want to have them connected to more than one statement, you use the block statement ({...}
) to do that.
Many, many, many people always use the block statement even when they could get away with not using it, both for clarity and to make it easier to add a second thing into the block.