JavaScript Return Values

By

Published · Updated

Tagged: JavaScript

Learn how return values work in JavaScript, why every function returns undefined by default, and how the return keyword ends a function and hands back a value.

Every function returns a value, which by default is undefined.

Undefined return value

Any function is terminated when its lines of code end, or when the execution flow finds a return keyword.

When JavaScript encounters this keyword it exits the function execution and gives control back to its caller.

If you pass a value, that value is returned as the result of the function:

const dosomething = () => {
  return 'test'
}
const result = dosomething() // result === 'test'

A return with no value also returns undefined:

function check() {
  return
}

check() // undefined

return stops the function

Nothing after a return runs. This makes return great for early exits, so you don’t need to nest everything in if/else:

function displayName(user) {
  if (!user) return 'Guest'

  return user.name
}

displayName(null) // 'Guest'
displayName({ name: 'Flavio' }) // 'Flavio'

The one exception is a finally block. If return happens inside a try, the finally code still runs before the function exits.

Returning multiple values

You can only return one value.

To simulate returning multiple values, you can return an object literal, or an array, and use a destructuring assignment when calling the function.

Using arrays:

Destructuring using arrays

Using objects:

Destructuring using objects

I use an array when the order is obvious and there are just 2 values, like [value, setValue]. I use an object when there are more values, because you pick them by name and the order doesn’t matter.

Arrow functions and implicit return

An arrow function without curly braces returns its expression automatically:

const double = (n) => n * 2

double(3) // 6

Add curly braces and that’s gone. Now you need return:

const double = (n) => {
  n * 2
}

double(3) // undefined

Returning an object has a gotcha. The curly braces of the object are read as the function body:

const getUser = () => { name: 'Flavio' }
getUser() // undefined

Wrap the object in parentheses:

const getUser = () => ({ name: 'Flavio' })
getUser() // { name: 'Flavio' }

Don’t put a new line after return

This one is sneaky:

function getUser() {
  return
  {
    name: 'Flavio'
  }
}

getUser() // undefined

JavaScript inserts a semicolon right after return when the line ends there (automatic semicolon insertion), so the function returns undefined and the object is never reached.

Keep the value, or at least its opening brace or parenthesis, on the same line as return:

function getUser() {
  return {
    name: 'Flavio'
  }
}

return inside forEach doesn’t return from your function

A return only exits the function it’s in. Inside a forEach callback, it exits the callback, not the outer function:

function findFirstEven(list) {
  list.forEach((n) => {
    if (n % 2 === 0) return n
  })
}

findFirstEven([1, 2, 3, 4]) // undefined

Use a for...of loop, where return exits the whole function:

function findFirstEven(list) {
  for (const n of list) {
    if (n % 2 === 0) return n
  }
  return null
}

findFirstEven([1, 3, 4, 6]) // 4

Or use the array method made for this: list.find((n) => n % 2 === 0).

async functions return a promise

An async function always returns a promise, even when you return a plain value:

async function getNumber() {
  return 42
}

getNumber() // Promise { 42 }
await getNumber() // 42

To get the value, await the call or use .then().

Want me to talk about your product? You can sponsor this site.

Related posts about js: