1

It seems to me that anytime a variable is declared, it should use the var keyword (unless it's specifically trying to access a global variable). Particularly because local variables can be collected when their function exits. But does it make any difference in something like a for loop?

for(var i = 0; i < ...; i++)

Generally, is there any standard best practice about the use of var keyword?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ab11
  • 19,770
  • 42
  • 120
  • 207
  • Yes, it's good practice to use the `var` keyword, always! Not doing so creates a global, which one generally doesn't want to do, and that applies to inside a `for` statement as well. – adeneo Apr 18 '15 at 15:54
  • Always declare variables with `var`. Use "strict" mode by including the line `"use strict";` at the top of your script blocks and/or namespace wrapper functions to catch inadvertent assignments without `var` (and typos etc). – Pointy Apr 18 '15 at 15:54

3 Answers3

2

Generally, is there any standard best practice about the use of var keyword?

Yes there is. And that is to use var every time.

But does it make any difference in something like a for loop?

There is no block-scope in javascript yet. So it makes no difference whatsoever. It is same as

var i; 
for(i = 0; i < 10; i++) {
  // you can access i inside
}  // as well as outside the loop

Declaring variables without var keyword throws an error when used in 'use strict'. So it's always better to use var keyword and avoid the ambiguity by attaching a property to window if you want it to be global.

Ah forgot about let keyword, which will make its way in ES6, which allows for a block scope. So it can be written as

for(let i = 0; i < 10; i++){
   // i can be accessed here but not outside this block
}

Also it's worth noting is that

MDN: First, strict mode makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work" (although future failure is possible: likely, in modern JavaScript)

which means, that it is considered an accidental mistake if you omit var keyword and omitting it might not work in the future iterations of ECMAScript aka Javascript.

Use var and you'll not pollute global context, your variables will be context-aware and be fail-proof for future versions of javascript, or in MDN's words, modern JavaScript.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • 1
    "There is no block-scope in javascript" - not true for ES6! :) Don't forget about [let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) – bvaughn Apr 18 '15 at 15:57
  • @brianvaughn I didn't add it as it was not yet available, thanks though for pointing. Edited to include it. – Amit Joki Apr 18 '15 at 16:01
1

In Javascript, the var keyword appears to be optional. This is incorrect.

With var, a variable is created in the current scope. Without it, the variable is attached to the global scope. (This is window in the browser).

You should absolutely use var wherever possible. If you mean to intentionally attach a variable to the window, state that directly:

window.foobar = 'some text';

Declaring a variable in a for-loop is the same as declaring a variable anywhere else.

I doubt you want every loop in your program all trying to use window.i the same time, which is what will happen if you don't use var.

JoshWillik
  • 2,624
  • 21
  • 38
0

The comments are partially wrong.

Only functions have scope in javascript, so a for loop var has no actual functional difference. Using var in for loops does create the semantic implication that the value shouldn't be used later, though.

for(i = 0; i < 5; i++){}console.log(i);
for(var j = 0; j < 5; j++){}console.log(j);
q = 17
for(var q = 0; q < 5; q++){}console.log(q);

All 3 output 5 but with a var declaration I would not expect a future user of the for loop variable.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406