What if your code could hand you answers like a helpful friend instead of just doing chores silently?
Why Return values in Javascript? - Purpose & Use Cases
Imagine you ask a friend to do a quick math problem for you, but instead of telling you the answer, they just say "Done!" and walk away. You have to guess the result yourself or do the math again.
Without return values, functions can only do things but not give back results. This means you must repeat work or keep extra notes, which is slow and confusing. It's like baking a cake but never getting to eat it.
Return values let functions send back answers after doing their job. This means you get the exact result you want, ready to use right away, making your code clear and efficient.
function add(a, b) { console.log(a + b); } add(2, 3); // prints 5 but no value returnedfunction add(a, b) { return a + b; } let sum = add(2, 3); console.log(sum); // prints 5 and stores valueReturn values let your code pass results around easily, making programs smarter and more flexible.
Think of a vending machine: you put money in, press a button, and it returns your snack. Without the snack coming back, the machine wouldn't be very useful!
Return values let functions give back results.
This avoids repeating work and keeps code clean.
They help programs communicate and work together smoothly.
Practice
return statement do in a JavaScript function?Solution
Step 1: Understand the purpose of
Thereturnreturnstatement sends a value back to the place where the function was called.Step 2: Recognize function behavior after
Afterreturnreturn, 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 CQuick Check:
Return sends value back [OK]
- Thinking return prints output
- Believing return pauses function
- Confusing return with variable creation
Solution
Step 1: Identify correct return syntax
The correct syntax to return a value isreturn value;with a semicolon.Step 2: Check each option
return 10; usesreturn 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 AQuick Check:
Correct return syntax ends with semicolon [OK]
- Using colon instead of semicolon
- Confusing arrow functions with return syntax
- Using = for assignment with return
function add(a, b) {
return a + b;
}
console.log(add(3, 4));Solution
Step 1: Understand function behavior
The functionaddtakes two numbers and returns their sum usingreturn a + b;.Step 2: Calculate the return value
Callingadd(3, 4)returns 3 + 4 = 7, which is printed byconsole.log.Final Answer:
7 -> Option AQuick Check:
Sum of 3 and 4 is 7 [OK]
- Concatenating numbers as strings
- Forgetting to return value
- Expecting undefined without return
function greet(name) {
console.log('Hello ' + name);
return;
console.log('Goodbye');
}Solution
Step 1: Analyze function flow
The function prints 'Hello ' plus the name, then hitsreturn;which stops the function.Step 2: Check code after return
Code afterreturn(printing 'Goodbye') never runs, so 'Goodbye' is never printed.Final Answer:
The function returns before printing 'Goodbye', so 'Goodbye' never shows. -> Option DQuick Check:
Code after return is skipped [OK]
- Expecting code after return to run
- Thinking missing return value causes error
- Confusing console.log with return
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 BQuick Check:
Return inside loop stops function immediately [OK]
- Using forEach with return expecting to exit function
- Returning empty string instead of non-empty
- Returning last instead of first non-empty string
Ready to prove it?
Pass the test and this pattern moves unknown
