Chaining assignments in var declarations.

suggest change

Chaining assignments as part of a var declaration will create global variables unintentionally.

For example:

(function foo() {    
    var a = b = 0;
})()
console.log('a: ' + a);
console.log('b: ' + b);

Will result in:

Uncaught ReferenceError: a is not defined
'b: 0'

In the above example, a is local but b becomes global. This is because of the right to left evaluation of the = operator. So the above code actually evaluated as

var a = (b = 0);

The correct way to chain var assignments is:

var a, b;
a = b = 0;

Or:

var a = 0, b = a;

This will make sure that both a and b will be local variables.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Anti-patterns:
* Chaining assignments in var declarations.

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
83 Anti-patterns
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde