JS-050

Beginner4 min~4 min read#early-return#return#functions
Early Return
Return a result early
Goal
Learn to return a result immediately when the answer is already known.
Explanation
- Early Return means returning a result before the end of a function.
- If the answer is already known, there is no reason to run the remaining code.
- return ends the function immediately.
- This makes code simpler and quicker to read.
- Early Return is used very often in real-world projects.
Analogy
If a teacher can already see that a test has failed, they record the result immediately instead of checking the remaining pages.
Code
function check(score) {
if (score < 50) {
return "Fail";
}
return "Pass";
}Result: Fail
Common mistake
Beginners write many unnecessary checks after the result is already known.
Remember
- ✦ return ends a function.
- ✦ If the answer is known, return it immediately.
- ✦ Do not run unnecessary code.
Quick Quiz
What does return do?