Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript - what do multiple commas do in a var statement [duplicate]

I have been looking through new wordpress theme twentyfourteen and notice the following line in their javascript file:

var nav = $( '#primary-navigation' ), button, menu;

Can somebody explain me what does it mean, it does not look like a multiple variable assignment in a single line. Also button and menu are not defined yet, so how come it does not produce an error?

like image 903
Tamik Soziev Avatar asked Aug 05 '26 11:08

Tamik Soziev


1 Answers

You are just declaring three variables and assigning value to only nav. Both button and menu will have undefined, by default.

var a = 1, b, c;
console.log(a, b, c);

Output

1 undefined undefined

Instead, if you see something like this

var d = (1, 2, 3);
console.log(d);

Output

3

The expressions within the brackets will be evaluated from left to right and the result of the last evaluation will be assigned to d.

like image 146
thefourtheye Avatar answered Aug 07 '26 00:08

thefourtheye