In Javascript – what do multiple commas do in a var statement

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?

Related posts

Leave a Reply

3 comments

  1. 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.

  2. This is equivalent to the following:

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

    button and menu are undefined and are typically referenced in your example as variables that are used later throughout the script.