Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 7.69

bookReturn Values

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
some-alt