Getting comfortable with the shorthand conditional syntax. If you put the conditional on one line, the {}
are not needed.
Even when having multiple conditionals.
return
will terminate the function and spit out as it sees. No need to assign the argument to a variable.
function InTheBeginning(reality) {
if (reality) return "God created the heavens and earth.";
else if (!reality) return "A chaotic destructive event defied science and beauty.";
return "Wish we could return to more shallow topics, but this is not returned.";
}
InTheBeginning(true);
=> "God created the heavens and earth".
InTheBeginning(false);
=> "A chaotic destructive event defied science and beauty."
Maybe you don't want to have a return to interrupt the sequence of the function. You could assign a value to a variable and return the variable at the end.
function InTheBeginning(reality) {
var belief;
if (reality) belief = "God created the heavens and earth.";
else if (!reality) belief = "A chaotic destructive event defied science and beauty.";
return belief;
}
InTheBeginning(true);
=> "God created the heavens and earth."
InTheBeginning(false);
=> "A chaotic destructive event defied science and beauty."
If you are using the variable approach. Be careful to notice if there are any return values before returning a variable. Returns exit the program immediately.
function InTheBeginning(reality) {
var belief;
if (reality) belief = "God created the heavens and earth.";
else if (!reality) belief = "A chaotic destructive event defied science and beauty.";
return "Why can't we return to shallow topics?";
return belief;
}
InTheBeginning(true);
=> "Why can't we return to shallow topics?"
function InTheBeginning(reality) {
var belief;
if (reality) belief = "God created the heavens and earth.";
else if (!reality) belief = "A chaotic destructive event defied science and beauty.";
else return "Why can't we return to shallow topics?";
return belief;
}
InTheBeginning(true);
=> "God created the heavens and earth."
This makes logical sense, but it's good to note the syntax. I wasn't paying attention and thought that my return in the following else
clause would have been the functions return value. It's because the previous was checking boolean and the if/elseif/else was met at a higher level, so the conditional never hit the else. The else had a return, but it wasn't called.
The return is only returned when it's called. Not just becuase it's in the code.
I figured it out when I moved the conditionals from boolean, to checking strings; I was thinking that the origins()
call, while true would have returned 'open for discussion' but the reason it didn't, was because the conditional had been met in the if
block and moved out of that sequence and moved to the return
value.
function origins(belief_system) {
var _beliefs;
if (belief_system == "created") _beliefs = "Intelligent Design.";
else if (belief_system == "chance") _beliefs = "Evolution.";
else return "Open for discussion.";
return _beliefs;
}
origins("created"); // showed me that the 'if/else if / else' conditional was met so it stopped moving down the chain.
=> Intelligent Design
I also noticed that a return in the else can prevent a possible, undesired 'undefined'.
function origins(belief_system) {
var _beliefs;
if (belief_system == "created") _beliefs = "Intelligent Design.";
else if (belief_system == "chance") _beliefs = "Evolution.";
else return "Open for discussion.";
return _beliefs;
}
origins("confused");
=> "Open for discussion."
function origins(belief_system) {
var _beliefs;
if (belief_system == "created") _beliefs = "Intelligent Design.";
else if (belief_system == "chance") _beliefs = "Evolution.";
else "Open for discussion."; // removed the explicit return
return _beliefs;
}
origins("confused");
=> undefined
If you don't put a return
, then the return of _beliefs
is undefined
.