Function parameters or no parameters in JavaScript code
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.
javascript function
add a comment |
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.
javascript function
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.
– Pointy
Nov 26 '18 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
– joyBlanks
Nov 26 '18 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.
– mcpolo
Nov 26 '18 at 15:54
add a comment |
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.
javascript function
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.
javascript function
javascript function
edited Nov 26 '18 at 16:16
Nela Jungmannová
asked Nov 26 '18 at 15:48
Nela JungmannováNela Jungmannová
287
287
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.
– Pointy
Nov 26 '18 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
– joyBlanks
Nov 26 '18 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.
– mcpolo
Nov 26 '18 at 15:54
add a comment |
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.
– Pointy
Nov 26 '18 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
– joyBlanks
Nov 26 '18 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.
– mcpolo
Nov 26 '18 at 15:54
4
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.
– Pointy
Nov 26 '18 at 15:50
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.
– Pointy
Nov 26 '18 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
– joyBlanks
Nov 26 '18 at 15:52
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
– joyBlanks
Nov 26 '18 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.
– mcpolo
Nov 26 '18 at 15:54
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.
– mcpolo
Nov 26 '18 at 15:54
add a comment |
2 Answers
2
active
oldest
votes
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))
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
add a comment |
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 35add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484693%2ffunction-parameters-or-no-parameters-in-javascript-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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))
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
add a comment |
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))
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
add a comment |
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))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))function idealHours(idealHours=8){
return idealHours*8;
}
console.log("Without parameters",idealHours())
console.log("With parameters",idealHours(3))function idealHours(idealHours=8){
return idealHours*8;
}
console.log("Without parameters",idealHours())
console.log("With parameters",idealHours(3))edited Nov 26 '18 at 15:57
answered Nov 26 '18 at 15:53
AlexisAlexis
4,63911536
4,63911536
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
add a comment |
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
BTW, wouldn't it be an advanced concept for OP. Realizing from the post. You may link the documentation for such reason.
– Bhojendra Rauniyar
Nov 26 '18 at 15:56
add a comment |
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 35add a comment |
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 35add a comment |
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 35The 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///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///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 35answered Nov 26 '18 at 16:03
Calixte SimeonCalixte Simeon
497
497
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53484693%2ffunction-parameters-or-no-parameters-in-javascript-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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.
– Pointy
Nov 26 '18 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
– joyBlanks
Nov 26 '18 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.
– mcpolo
Nov 26 '18 at 15:54