What is the purpose of the return statement in JavaScript?
a) To exit a function and return a value
b) To stop a loop
c) To repeat a function
d) To declare a variable
Answer:
a) To exit a function and return a value
Explanation:
The return
statement is used inside a function to stop its execution and return a value to the calling code. If a function does not have a return
statement, it returns undefined
by default.
Example:
function add(a, b) {
return a + b;
}
console.log(add(5, 10)); // Output: 15