What is the scope of several vars defined on the same line?

Simple question. Today I wrote something simple:

var deferred = null;
var user = null;
var campaigns = null;

But my editor suggested that I put the definition of these vars on the same line, for example:

var deferred = user = campaigns = null;

Are the scope of the two definitions the same?

I was wondering if, with the same string definition, make the user and campaigns global rather than local to the function where they were defined.

+4
source share
2 answers

Warning ... you are not declaring three variables. You declare only one ( deferred). This will mean that others will leak into the global area (or reset the reference error in strict mode).

First you need to declare all three variables:

var deferred, user, campaigns;
deferred = user = campaigns = null;

, :

(function () {
    "use strict";
    var x = y = null; // Throws a reference error
}());

, var ( ... null? undefined ):

var deferred = null,
    campaigns = null,
    user = null;
+8

, javascript , , , . :

function test() { 
    var i;

    i = 5;
    console.log(i);
    // Some more code code 

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

JavaScript :

function test() { 
    var i;
    var j;
    var k;        

    i = 5;
    console.log(i);
    // Some more code code 

    for (j = 0; j < i; j++) {
        k = i + j;
        console.log(j);
    }
}

( @Alex K, hoisting).

javascript , , - jslint .

, :

var i, j;

var i;
var j;

,

var i = 0, j = 3;

, .

javascript- Javascript Douglas Crockford (, ..)

+2

Source: https://habr.com/ru/post/1523364/