Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

Return Values

Learn how a JavaScript function can return a value, how the return statement works, and how returned values can be stored and reused.

What is a Return Value?

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

In JavaScript, we use the return keyword when a function needs to give a value back to the caller.

The returned value can be stored in a variable, displayed, or used in another calculation.

JavaScript Return Statement and Return Value

JavaScript function returning a calculated value back to the function call.

Simple Explanation

Think of a function like a calculator. You give some input to the function, the function performs a task, and then it gives the result back.

For example, if a function adds 10 + 20, the function can return 30.

Basic Structure

function functionName() {
    return value;
}

const result = functionName();

Parts of a Return Value

1
Function

The function contains the statements that perform a particular task.

2
return Keyword

The return keyword sends a value back to the caller.

3
Returned Value

The returned value can be stored and reused in the program.

How Return Value Works

Function Call → Execute → Return → Store Result
Function Call addNumbers(10, 20)
→
Function Executes 10 + 20
→
return return 30
→
Result const result = 30

Example 1: Returning a Value

The addNumbers() function adds two numbers and returns the result.

function addNumbers(a, b) {
    return a + b;
}

const result = addNumbers(10, 20);

console.log(result);

Output

30

Example 2: Returning a Calculated Value

A function can perform a calculation and return the calculated result.

function calculateTotal(price, quantity) {
    return price * quantity;
}

const total = calculateTotal(500, 3);

console.log("Total:", total);

Output

Total: 1500

Return vs console.log()

The return statement and console.log() have different purposes.

return

The return statement sends a value back to the caller. The returned value can be stored and used later.

console.log()

The console.log() statement displays information in the browser console. It does not send the value back to the caller.

Example 3: Using the Returned Value

A returned value can be stored in a variable and then used in another calculation.

function square(number) {
    return number * number;
}

const result = square(5);

const finalResult = result + 10;

console.log("Result:", result);
console.log("Final Result:", finalResult);

Output

Result: 25
Final Result: 35

Important Note

Code After return Does Not Execute

When JavaScript reaches a return statement, the function immediately stops executing and sends the value back to the caller.

Statements written after the return statement inside the same function will not execute.

Example 4: Returning a String

A function can also return a string value.

function getCourseName() {
    return "JavaScript Full Stack";
}

const course = getCourseName();

console.log(course);

Output

JavaScript Full Stack

Summary

A JavaScript function can return a value using the return keyword. The returned value can be stored in a variable and reused in another calculation or expression. Return values make functions reusable and allow them to produce results that can be used by other parts of the program.