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.
-
4In 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.Pointy– Pointy2018年11月26日 15:50:40 +00:00Commented 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 variableJoy Biswas– Joy Biswas2018年11月26日 15:52:20 +00:00Commented 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.fwjggYUKGFTYucfty– fwjggYUKGFTYucfty2018年11月26日 15:54:22 +00:00Commented Nov 26, 2018 at 15:54
2 Answers 2
In JS you can set a default value for your parameter. If you don't use the parameter the default value will be taken.
function idealHours(idealHours=8){
return idealHours*8;
}
console.log("Without parameters",idealHours())
console.log("With parameters",idealHours(3))
answered Nov 26, 2018 at 15:53
Alexis
5,8211 gold badge30 silver badges46 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Bhojendra Rauniyar
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
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
lang-js