What is the different between a parameter and a local variable?

前端 未结 4 821
你的背包
你的背包 2021-01-14 16:06

Apologies for what must seem like a very stupid question.

I\'m currently working through codecadamy, and this is throwing me off:

 var greet         


        
4条回答
  •  被撕碎了的回忆
    2021-01-14 16:23

    In your function definition:

    function(name) {
    

    name is already being declared. It is a parameter for the function. If you want to change name, you can, but you don't need to use var to declare it as a new variable.

    On the other hand, if you wanted to add, say, flavor, you should use var then since it is not already defined.

    var flavor = 'savory';
    

    In this case, flavor is a local variable and name is a parameter. A parameter is a type of local variable that is declared with the function declaration, but a local variable isn't necessarily a parameter because it may be declared elsewhere in the function.

提交回复
热议问题