Does this line create global variables?

Does this line create global variables?

var first=second=third=fourth=fifth="Hello, ";

      

I'm not really sure how to test it :)

+3


source to share


4 answers


first

will be declared as a local variable and the rest will be global.

To fix this, try the following:

var first, second, third, fourth, fifth;
first = second = third = fourth = fifth = "Hello, ";

      



Or all on one line:

var first, second, third, fourth, fifth = fourth = third = second = first = "Hello, ";

      

+3


source


Yes, everything except first

is global in this case. You can test your browser console by running

(function() { var x = y = 1; })();
console.log(y); // 1

      



You might want var y, x = y = 1;

+2


source


First will be local, the rest will be global. See this script from the JS Fiddle .

+2


source


Yes, this is to declare your variables first:

var first,second,third,fourth,fifth;
first=second=third=fourth=fifth="Hello, ";

      

Will do the same, except all the variables will be in the expected scope.

You only have a variable declaration with a name first

and four other variables are used without declaring them, so they are global.

+1


source







All Articles