Using a function variable vs. "direct return"

in Sleep Debt Calculator project, I wrote this function:

const getActualSleepHours = () => {
 return
  getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturday') +
  getSleepHours('sunday');
}

In this case, console.log(getActualSleepHours()); returns undefined.

Then I changed it to

const getActualSleepHours = () => {
  let ActualSleepHours =
  getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturday') +
  getSleepHours('sunday');
  return ActualSleepHours;
}

console.log(getActualSleepHours()); now returns 55, which is what I wanted.
Why did the first one not work?

…in the instructions, it says:

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 .

(emphasis mine). Maybe that has something to do with it - but what’s an implicit return?

Hi there.

In your original attempt:

const getActualSleepHours = () => {
 return
  getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturday') +
  getSleepHours('sunday');
}

you can’t just write return and then merrily move to a new line. JavaScript will interpret that exactly as written: to return nothing, hence undefined.

Your refactored code calculates the total into a new variable, let ActualSleepHours, and then returns that variable.

No, that has nothing to do with it.

An implicit return is a feature of arrow functions. It is essentially the ability to do this:

const addTwoNums = (num1,num2) => num1 + num2;
console.log(addTwoNums(2,5)); //output: 7

There is no return keyword anywhere in the addTwoNums function, hence it is an implicit return.

1 Like

And here I thought semicolons were mandatory inside of braces for the end of a statement (meaning where there’s no semicolon, javascript keeps looking for the end of a statement).
BUT if that’s not the case (ergo: line break means end of statement, even inside curly braces), then my second version shouldn’t work either, it should throw a syntax error because I didn’t put a semicolon in any of those lines…?

Generally speaking, any statement you write in JavaScript - whether that’s declaring/initialising a variable, expressions etc - ought to be terminated with a semi-colon.

JavaScript’s specification, however, includes a feature called automatic semicolon insertion which is intended to try and catch mistakes and fix them to keep your code running.

One such instance where a semicolon will be automatically inserted is if you write:

var foo = function() {
  var bar = 'baz'
  return 
  {
    bar: bar
  }
}

The format for a return statement is meant to be:
return [no LineTerminator] statement;
but here I’ve put a line terminator after the return, and then followed it with the statement I intended to return.

This is not allowed by JavaScript, as defined by the standard:

When a continue , break , return , throw , or yield token is encountered and a LineTerminator is encountered before the next token, a semicolon is automatically inserted after the continue , break , return , throw , or yield token.

Your second attempt is of the form:

const getActualSleepHours = () => {
  let ActualSleepHours =
  getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturday') +
  getSleepHours('sunday');
  return ActualSleepHours;
}

This works fine, even though your assignment statement is written across several lines, because it is allowed by JavaScript’s grammar and so there’s no automatic semicolon insertion at the end of let ActualSleepHours =.

:slight_smile:

1 Like