JAVASCRIPT Tutorial

Return Values

What is a Return Value?

A return value is the data that a function sends back to the code that called it.

How to Return a Value

To return a value from a function, use the return keyword followed by the value.

function sum(a, b) {
  return a + b;
}
Data Type

The return value can be any data type, including primitives (e.g., numbers, strings) or complex objects (e.g., arrays, objects).

Undefined

if a function doesn't explicitly return a value, it returns undefined.

Example in JavaScript

function double(x) {
  return x * 2;
}

const result = double(5); // result = 10

Key Concepts

  • return keyword: Used to send data back to the calling code.
  • Data type: The data type of the returned value.
  • Undefined: The default return value when a function doesn't return anything.