Skip to content

Instantly share code, notes, and snippets.

@fakefarm
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakefarm/d175d16643b24249d3f2 to your computer and use it in GitHub Desktop.
Save fakefarm/d175d16643b24249d3f2 to your computer and use it in GitHub Desktop.

Shorthand if, else if, else (and return)

Getting comfortable with the shorthand conditional syntax. If you put the conditional on one line, the {} are not needed. Even when having multiple conditionals.

Explicit returns

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."

Using a var

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."

Possible gotcha

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?"

A return is not called on a false conditional

  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

Another gotcha

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment