2

In an HTML <script> tag, outside of any function, does var have any meaning?

Is there any difference between these three notations?

<script>
  var a = 1;
  b = 1;
  window.c = 1;
</script>
Cilan
  • 13,101
  • 3
  • 34
  • 51
AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • No, in the global scope it doesn't because you're defining it locally in the global scope which is the same thing as defining it in the global scope without `var`. – Cilan Dec 02 '14 at 03:46
  • Using `var` is faster, however: http://jsperf.com/var-in-global-scope (tiny micro-optimization, you shouldn't sacrifice quality in your code) – Cilan Dec 02 '14 at 03:50
  • @TheWobbuffet— the *var* keyword has exactly the same meaning in global code as it does in function code—it creates a variable in the current execution context. There are at least two significant differences between declared variables and properties assigned to the global (window) object. – RobG Dec 02 '14 at 04:39
  • @RobG Yes, so I assume not using var is slower in the global scope because it has to keep going up on the hierarchy until it finds the global object – Cilan Dec 02 '14 at 13:40

2 Answers2

1

At the top level (outside all functions) var declares a global variable.

To avert this, wrap your code in a function.

(function () {
    var a = 1;
}());

'a' in window;
// => false

Despite that each form declares a global variable, they have subtle differences. var creates an undeletable property on window, and is subject to typical hoisting rules whereby the property will exist on window at the start of the var statement's execution context. The other forms create a deletable property on window at the time that the line of code is executed.

See this answer for more info.

Community
  • 1
  • 1
Jackson
  • 9,188
  • 6
  • 52
  • 77
  • Can you elaborate? "Is there any difference between these three notations?" – redditor Dec 02 '14 at 03:42
  • 1
    They are the same in that context. – Jackson Dec 02 '14 at 03:42
  • Thanks. So you don't need var at all, outside a function? – redditor Dec 02 '14 at 03:43
  • 2
    Technically no, but I recommend the third form, because the intent of the code is clearest. The first form implies the the variable should have been localized, and the second form is better used to reference variables which are already defined. – Jackson Dec 02 '14 at 03:44
  • @RobG You're right. The statement "They are the same in that context." is not true. I have updated my post to elaborate on this. – Jackson Dec 02 '14 at 05:10
0

In your simple, unlikely example, there is no difference, but there are a few subtle differences that can result in hair-pulling if you decide to make a habit of not declaring variables "since they are the same."

You can read up on these differences at the Mozilla Developer Network, my go-to on questions like this.

Summarizing briefly the main reasons b = 1 is bad:

  • Browsers in strict mode will throw an exception on b = 1.

  • From within a function, only var a = 1 will be scoped to the function. It will be unclear whether you intend for b = 1 to be global or not. If you're mutating that variable, one day you're going to wonder why the heck you're getting different results from the same exact function with the exact same parameters.

Chad Hedgcock
  • 11,125
  • 3
  • 36
  • 44