Function parameters or no parameters in JavaScript code












1















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.










share|improve this question




















  • 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
















1















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.










share|improve this question




















  • 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














1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















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))








share|improve this answer


























  • 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



















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








share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    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))








    share|improve this answer


























    • 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
















    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))








    share|improve this answer


























    • 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














    1












    1








    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))








    share|improve this answer















    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))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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













    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








    share|improve this answer




























      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








      share|improve this answer


























        0












        0








        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








        share|improve this answer













        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








        ///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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 16:03









        Calixte SimeonCalixte Simeon

        497




        497






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista