Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Return Values | Working with Function Data and Scope
Functions in JavaScript

bookReturn Values

The return statement is a fundamental feature in JavaScript functions. It allows a function to send a value back to the part of the program where it was called. When you use return, the function stops executing and immediately provides the specified value to the caller. This is how functions can produce results that can be stored in variables, used in expressions, or passed to other functions. Without a return statement, a function completes its instructions but does not provide any value back, so it returns undefined by default.

123456
function sum(a, b) { return a + b; } const result = sum(3, 7); console.log("The sum is:", result);
copy

Some functions are designed to produce a result and return it, while others simply perform an action without returning anything. When a function has a return statement with a value, you can use that value elsewhere in your code. If a function does not explicitly return a value, JavaScript automatically returns undefined. Functions that return values are useful for calculations, data processing, and any situation where the result needs to be reused. Functions without return values are often used for tasks like logging, updating the user interface, or triggering side effects.

12345678910111213141516171819
function calculator(a, b, operation) { if (operation === "add") { return a + b; } else if (operation === "subtract") { return a - b; } else if (operation === "multiply") { return a * b; } else if (operation === "divide") { return a / b; } else { return "Unknown operation"; } } console.log("Add:", calculator(4, 2, "add")); console.log("Subtract:", calculator(4, 2, "subtract")); console.log("Multiply:", calculator(4, 2, "multiply")); console.log("Divide:", calculator(4, 2, "divide")); console.log("Unknown:", calculator(4, 2, "modulo"));
copy
question mark

Which of the following statements about JavaScript function return values are true?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what happens if I remove the return statement from a function?

What does the calculator function return if an unknown operation is provided?

Can you give more examples of functions that don't use return statements?

Awesome!

Completion rate improved to 7.69

bookReturn Values

Swipe to show menu

The return statement is a fundamental feature in JavaScript functions. It allows a function to send a value back to the part of the program where it was called. When you use return, the function stops executing and immediately provides the specified value to the caller. This is how functions can produce results that can be stored in variables, used in expressions, or passed to other functions. Without a return statement, a function completes its instructions but does not provide any value back, so it returns undefined by default.

123456
function sum(a, b) { return a + b; } const result = sum(3, 7); console.log("The sum is:", result);
copy

Some functions are designed to produce a result and return it, while others simply perform an action without returning anything. When a function has a return statement with a value, you can use that value elsewhere in your code. If a function does not explicitly return a value, JavaScript automatically returns undefined. Functions that return values are useful for calculations, data processing, and any situation where the result needs to be reused. Functions without return values are often used for tasks like logging, updating the user interface, or triggering side effects.

12345678910111213141516171819
function calculator(a, b, operation) { if (operation === "add") { return a + b; } else if (operation === "subtract") { return a - b; } else if (operation === "multiply") { return a * b; } else if (operation === "divide") { return a / b; } else { return "Unknown operation"; } } console.log("Add:", calculator(4, 2, "add")); console.log("Subtract:", calculator(4, 2, "subtract")); console.log("Multiply:", calculator(4, 2, "multiply")); console.log("Divide:", calculator(4, 2, "divide")); console.log("Unknown:", calculator(4, 2, "modulo"));
copy
question mark

Which of the following statements about JavaScript function return values are true?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt