Curly brace ==> if/else


#1

Here is the project Sleep Dept Calculator from Js
I’ve some question about if/else, down below is my code.
what is different if I take curly brace away?

const getActualSleepHours = () =>{
  getSleepHours('Monday')+
  getSleepHours('Tuesday')+
  getSleepHours('Wednesday')+
  getSleepHours('Thursday')+
  getSleepHours('Friday')+
  getSleepHours('Saturday')+
  getSleepHours('Sunday')
}
console.log(getActualSleepHours());
//result undefined

const getActualSleepHours = () =>
  getSleepHours('Monday')+
  getSleepHours('Tuesday')+
  getSleepHours('Wednesday')+
  getSleepHours('Thursday')+
  getSleepHours('Friday')+
  getSleepHours('Saturday')+
  getSleepHours('Sunday')

console.log(getActualSleepHours());
//result real number





#2

taking the curly brackets away will automatically return the result, its a shorthand for single line functions. Your function can be written as a single line, which is why it works

i still wouldn’t use this shorthand much, i would just add return keyword to the first code


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.