Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why does a return on its own line return undefined?

Do you know what will be the output of this code?

JavaScript
function get() {
  return
    42;
}

console.log(get());
Show answer and explanation

The answer is undefined.

JavaScript inserts a semicolon after a return that’s followed by a line break. The function returns undefined and 42 is never reached. Keep the value on the same line as return.

Take the full quiz Next question →

More Basics questions

  1. How do closures create private variables in JavaScript?
  2. How do you write a once() function with a closure?
  3. Can you call a function before it is declared in JavaScript?
  4. Why can't you call a function expression before it's defined?
  5. Can you change this on a bound function with call()?

Related topics