What is the a implicit return and how is it different from the non implicit return? Also, are there other types of returns we learn in JavaScript?

What is the a implicit return and how is it different from the non implicit return? Also, are there other types of returns we learn in JavaScript?

The opposite of implicit is explicit.

Explicit returns are really the bread and butter of most programming languages (including javascript). I don’t think I’ve ever written an implicit return other than for this example… but i see a lot of good use cases for it.

I think implicit returns in js have to do with arrow functions:

let func = x => x + x;                  
// implicit return (because no brackets for the function code)
func(5)
//returns 10

let func2 = (x, y) => { return x - y; }; 
// notice the brackets, and the explicit statement of return

func2(5,4)
//returns 1
2 Likes

Indeed. I think the idea is similar to Python’s lambda functions.

2 Likes

Thank you for translating it for me hahaha. Ahem…

2 Likes

So, can we just say an implicit return is implied? Meaning you don’t write the word return.

To say implicit is to mean implied. For “it’s an implicit return” you could also say “the return is implied”. It would be redundant to say “the implicit return is implied”.

ty thats what i meant. yours is much clearer.

perhaps you could help me with a question. How do create the javascript for a button that logs the current time and keeps track until you click the button again. I want to create a sleep debt button. the button would say sleep and change to wake when clicked and viceversa.

In the “Functions” cheatsheet, Arrow Functions (ES6), second sub-paragraph it says"…can use the concise function body…".

In the project "sleep-debt-calculator step 5 says 5. “Inside the getActualSleepHours() function, call the getSleepHours() function for each day of the week. Add the results together and return the sum using an implicit return .”

The Cheatsheet doesn’t mention “implicit” and the project doesn’t mention concise.

A concise function body consists of a single expression. In this instance the syntax rules for standard function are relaxed. That means no braces, and no return. The expression is the return.

const func = x => x ** 2;

The fat arrow points to the function body, the expression, x ** 2.

console.log(func(25))    //  625

‘Implicit’ means implied, as in the return is implied. These relaxed rules only apply when the function body is a single expression.

Consider the instructions when they say to add, that means one long expression of repeated calls separated by plus signs.

... => getSleepHours(...) + getSleepHours(...) + ...;