Return 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.
123456function sum(a, b) { return a + b; } const result = sum(3, 7); console.log("The sum is:", result);
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.
12345678910111213141516171819function 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"));
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 7.69
Return 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.
123456function sum(a, b) { return a + b; } const result = sum(3, 7); console.log("The sum is:", result);
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.
12345678910111213141516171819function 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"));
Danke für Ihr Feedback!