Return early pattern for functions

Dec 5, 2017 00:00 · 61 words · 1 minute read

When a first return statement is reached, the execution of the current function stops and control returns to the calling location. Example:

function myFunc() {
    console.log("Salam alaykum");
    return "Wa alaykum salam";
    console.log("Ciao");
}

myFunc();

The output “Salam alaykum” to the console, returns “Wa alaykum salam”, but “Ciao” is never output, because the function exits at the first return statement.

Ref freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/return-early-pattern-for-functions