JavaScript/Reserved words/return

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Previous: public Reserved words Next: short

The return keyword[edit | edit source]

The return keyword ends the function, specifying the value to be returned. Without this expression, undefined is returned.

Please note that adding a carriage return inbetween the statement and the value results in splitting it into two separate statements, returning with undefined. This is because of the automatic semicolon insertion (ASI).

Examples[edit | edit source]

Example 1
  function getValue() {
    return value;

The getter getValue returns with value.

Example 2
  function getName() {
    return   // <<<< == "return undefined;"
      name;  // <<<< unreachable code!

The getter getName returns with undefined.

Previous: public Reserved words Next: short