Return Values

Returning data from functions.

Examples

Basic Return

Returning the product of two numbers.

function myFunction(a, b) {
  return a * b;
}
let x = myFunction(4, 3); // x will be 12

Return Ends Execution

Code after the return statement is unreachable.

function test() {
  return true;
  console.log("This will never be printed");
}
test();

Conditional Return

Returning different values based on a condition.

function checkAge(age) {
  if (age >= 18) {
    return true;
  } else {
    return false;
  }
}
console.log(checkAge(20)); // true

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.