Domanda

The teachers at my school are teaching us we have to specifically declare function variables like so:

function something(var1, var2)
{
    var var1, var2;
    console.log(var1);
    console.log(var2);
}

something("Totally", "Cool");

However I can't seem to find any topics about this, so my question is, is this even required? Isn't it perfectly normal and OK to just leave the var var1, var2; out?

If so, is this stated somewhere in the W3C JavaScript documentation?

I mean it works either way, but if it works without... Cleaner code I'd say :P

Code I'm talking about: http://pastebin.com/y5B2aznc

Thanks!

È stato utile?

Soluzione 2

In your particular case, all the variables passed as arguments are local variables just like the variables you declare with var. So re-declaring them with var is redundant.

But a better practice is to leave the arguments with their original values, and do stuff with them storing the results in other local variables. E.g.:

function something(var1, var2)
{
    var varAlt1 = var1.toUpperCase(), varAlt2 = var2.toLowerCase();
    console.log(varAlt1);
    console.log(varAlt2);
}

Altri suggerimenti

in javascript, the scope of variables depends on how they are declared:

  • if you declare the variable when you define it, the scope of the variable is global
  • if you declare it using the var keyword, the scope of the variable is within the function
  • if you use the newer ES6 let keyword, the scope of the variable is within current block

so it is better to not use global variables and always limit the scope of your variables.

Here are a few references explaining the variable scopes in javascript:

you should also definitely read Douglas Crockford's Javascript, the good parts:

edit:

in your particular case, it is not useful:

function something(var1, var2)
{
    var var1, var2;
    console.log(var1);
    console.log(var2);
}

as you give those variables as parameters of the function, they are already declared within the function scope, so declaring them again as var is redundant, and you can safely remove that line.

function something(var1, var2)
{
    console.log(var1);
    console.log(var2);
}

Omitting the keyword var in the declaration will result in the variables being global and not having their scope limited to the function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

If you don't use the var statement then the variables will be created in the global scope which is the window object. This is a bad practice since you're polluting the global scope and can affect other scripts/functions.

I suggest you read on the var statement on MDN. Also, here's a nice article on Javascript scope.

Edit: I didn't notice that the variables you're declaring are also the function parameters. In this case, the var statement will have no effect since re-declaring an already declared variable is a no-op (does nothing).

If you want to know more about the var statement and how scope is handled in Javascript, may I suggest reading this article?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top