2

Why am I taught to write this (basic) function with no parameter?

function idealSleepHours () {
    const idealHours = 8;
    return idealHours*7
};

It is working with parameter as well. Am I missing something?

   function idealSleepHours (idealHours) {
      idealHours = 8;
      return idealHours * 7
    };

I am sorry for a dumb question, I am new in JavaScript (and programming), therefore everything is a little bit confusing for me.

Edit: Thank you very much for your answers, now I absolutely understand the difference.

3
  • 4
    In that particular function, it makes no difference functionally, though the second version with the parameter makes less sense because the value of the parameter is always ignored. Parameters are useful when the values are not ignored. Commented Nov 26, 2018 at 15:50
  • In second case idealHours is local scoped so if you call idealSleepHours(0) it will be 0 else it will be undefined, doesnt matter as your line one is overriding the variable Commented Nov 26, 2018 at 15:52
  • Like @Pointy said, you don't need the parameter in the second function. But, if you take out the line second line, you can pass a value to idealHours when you call the function elsewhere in the program. Commented Nov 26, 2018 at 15:54

2 Answers 2

1

In JS you can set a default value for your parameter. If you don't use the parameter the default value will be taken.

Documentation

function idealHours(idealHours=8){
  return idealHours*8;
}

console.log("Without parameters",idealHours())

console.log("With parameters",idealHours(3))

Sign up to request clarification or add additional context in comments.

1 Comment

BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
0

The point here is always try to be flexible when building an application

///this function will always return the same result
//because you hard coded the variable inside the function
///so essential isn't an ideal design for a function unless
///it made logical since in your application
function idealSleepHours () {
    const idealHours = 8;
    return idealHours*7
};



/*In the function below you are passing a parameter into the function
which is a  better design of a function, creating flexiablity.
but the problem is you again hard coded the value of idealhours in your function
so there is no need to set it to 8 inside the function 
*/
   function idealSleepHours (idealHours) {
      idealHours = 8;///this line should be omitted
      return idealHours * 7
    };

///making a much better function known as a pure function..

   function idealSleepHours (idealHours) {
      return idealHours * 7
    };
///now you can assign your ideal hours based on any case that might come into mind

idealSleepHours(8)///return 56
idealSleepHours(2)//return 14
idealSleepHours(5)//returns 35

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.