Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Return Values | Working with Function Data and Scope
Quizzes & Challenges
Quizzes
Challenges
/
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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

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?

bookReturn Values

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt