Introduction

The return keyword returns control to the calling function/method often with a value at the end of the function or method or undefined.

Example

function add (num, num2) {
  return num + num2;
}

// calling the add function
const sum = add(21, 24)
console.log(sum) // prints 45, sum now holds value 45

Execution of a function terminates at a return keyword or the enclosing } if no return keyword is provided. The function is popped off the stack and execution is returned to the calling function. Often return is used to return a value after function execution such as fetching data etc.

If no return keyword is provided, the execution of the function terminates at the last } with an undefined value.


Found this article helpful? You may follow me on Twitter where I tweet about interesting topics on software development.