January 2020
Intermediate to advanced
548 pages
13h 36m
English
Returning is a shift of control from a function to its caller. It is achieved either via an explicit return statement within the function itself or implicitly when the function runs to completion:
function sayHiToMe(name) { if (name) { return `Hi ${name}`; } // In the case of a truthy `name` this code is never arrived at // because `return` exists on a previous line: throw 'You do not have a name! :(';}sayHiToMe('James'); // => "Hi James"
Here, you'll notice that we don't bother placing the implied else condition of a falsy name in its own else block (else {...}) as this would be unnecessary. Because we return when the name is truthy, any code following that return statement will therefore only run in the implied else condition. ...
Read now
Unlock full access