What does the return statement do in a JavaScript function?
It stops the function and sends a value back to where the function was called.
Click to reveal answer
beginner
Can a JavaScript function return more than one value directly?
No, a function returns only one value. But you can return an object or array to hold multiple values.
Click to reveal answer
beginner
What happens if a function does not have a return statement?
The function returns undefined by default.
Click to reveal answer
beginner
How do you capture the value returned by a function?
You assign the function call to a variable, like <code>let result = myFunction();</code>
Click to reveal answer
beginner
What is the difference between console.log() and return in a function?
console.log() shows output on the screen but does not send a value back. return sends a value back to the caller.
Click to reveal answer
What does a return statement do inside a function?
AStops the function and sends a value back
BPrints a value to the console
CStarts the function
DDeclares a variable
✗ Incorrect
The return statement stops the function and sends a value back to where it was called.
What value does a function return if it has no return statement?
Anull
B0
Cundefined
Dfalse
✗ Incorrect
Functions without a return statement return undefined by default.
How can you return multiple values from a function?
AYou cannot return multiple values
BUse multiple <code>return</code> statements
CReturn a string with all values
DReturn an array or object containing the values
✗ Incorrect
You can return an array or object to hold multiple values from a function.
Which of these captures the returned value from a function?
Alet x = myFunction()
Bconsole.log(myFunction())
Creturn myFunction()
Dfunction myFunction()
✗ Incorrect
Assigning the function call to a variable captures the returned value.
What is the difference between return and console.log()?
A<code>return</code> prints output, <code>console.log()</code> sends value back
B<code>return</code> sends value back, <code>console.log()</code> prints output
CBoth do the same thing
DNeither sends value back
✗ Incorrect
return sends a value back to the caller; console.log() only prints to the console.
Explain what the return statement does in a JavaScript function and why it is useful.
Think about how functions give you answers.
You got /3 concepts.
Describe how you can return multiple values from a function in JavaScript.
Imagine putting things in a box to carry them all at once.
You got /3 concepts.
Practice
(1/5)
1. What does the return statement do in a JavaScript function?
easy
A. It pauses the function but continues later.
B. It prints a value to the screen.
C. It sends a value back to where the function was called.
D. It creates a new variable inside the function.
Solution
Step 1: Understand the purpose of return
The return statement sends a value back to the place where the function was called.
Step 2: Recognize function behavior after return
After return, the function stops running and no further code inside it executes.
Final Answer:
It sends a value back to where the function was called. -> Option C
Quick Check:
Return sends value back [OK]
Hint: Return sends value back and stops function [OK]
Common Mistakes:
Thinking return prints output
Believing return pauses function
Confusing return with variable creation
2. Which of the following is the correct syntax to return the value 10 from a function?
easy
A. return 10;
B. return = 10;
C. return: 10;
D. return => 10;
Solution
Step 1: Identify correct return syntax
The correct syntax to return a value is return value; with a semicolon.
Step 2: Check each option
return 10; uses return 10; which is correct. return = 10; uses equals sign for assignment, invalid. return: 10; uses colon which is invalid. return => 10; uses arrow syntax incorrectly.
Final Answer:
return 10; -> Option A
Quick Check:
Correct return syntax ends with semicolon [OK]
Hint: Use 'return value;' with semicolon [OK]
Common Mistakes:
Using colon instead of semicolon
Confusing arrow functions with return syntax
Using = for assignment with return
3. What is the output of this code?
function add(a, b) {
return a + b;
}
console.log(add(3, 4));
medium
A. 7
B. 34
C. undefined
D. NaN
Solution
Step 1: Understand function behavior
The function add takes two numbers and returns their sum using return a + b;.
Step 2: Calculate the return value
Calling add(3, 4) returns 3 + 4 = 7, which is printed by console.log.
Final Answer:
7 -> Option A
Quick Check:
Sum of 3 and 4 is 7 [OK]
Hint: Return adds numbers and prints result [OK]
Common Mistakes:
Concatenating numbers as strings
Forgetting to return value
Expecting undefined without return
4. Find the error in this function:
function greet(name) {
console.log('Hello ' + name);
return;
console.log('Goodbye');
}
medium
A. The function syntax is invalid.
B. Missing return value causes error.
C. The function should not use console.log.
D. The function returns before printing 'Goodbye', so 'Goodbye' never shows.
Solution
Step 1: Analyze function flow
The function prints 'Hello ' plus the name, then hits return; which stops the function.
Step 2: Check code after return
Code after return (printing 'Goodbye') never runs, so 'Goodbye' is never printed.
Final Answer:
The function returns before printing 'Goodbye', so 'Goodbye' never shows. -> Option D
Quick Check:
Code after return is skipped [OK]
Hint: Code after return does not run [OK]
Common Mistakes:
Expecting code after return to run
Thinking missing return value causes error
Confusing console.log with return
5. You want a function that returns the first non-empty string from an array or returns 'No valid string' if none found. Which function correctly uses return values?
hard
A. function firstNonEmpty(arr) {
arr.forEach(str => {
if (str) return str;
});
return 'No valid string';
}
B. function firstNonEmpty(arr) {
for (const str of arr) {
if (str) return str;
}
return 'No valid string';
}
C. function firstNonEmpty(arr) {
let result = '';
arr.forEach(str => {
if (str) result = str;
});
return result || 'No valid string';
}
D. function firstNonEmpty(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === '') return arr[i];
}
return 'No valid string';
}
Solution
Step 1: Understand the goal
The function must return the first non-empty string or a default message if none found.
Step 2: Evaluate each function
function firstNonEmpty(arr) {
for (const str of arr) {
if (str) return str;
}
return 'No valid string';
} uses a for-of loop and returns immediately when a non-empty string is found, else returns default. This is correct. function firstNonEmpty(arr) {
arr.forEach(str => {
if (str) return str;
});
return 'No valid string';
} uses forEach with return inside callback, which does not stop the outer function. function firstNonEmpty(arr) {
let result = '';
arr.forEach(str => {
if (str) result = str;
});
return result || 'No valid string';
} collects last non-empty string but does not return first. function firstNonEmpty(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === '') return arr[i];
}
return 'No valid string';
} returns empty string if found, which is opposite of requirement.
Final Answer:
function firstNonEmpty(arr) {
for (const str of arr) {
if (str) return str;
}
return 'No valid string';
} -> Option B
Quick Check:
Return inside loop stops function immediately [OK]
Hint: Use return inside loop to exit early [OK]
Common Mistakes:
Using forEach with return expecting to exit function
Returning empty string instead of non-empty
Returning last instead of first non-empty string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.