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?
You are just declaring three variables and assigning value to only
nav
. Bothbutton
andmenu
will haveundefined
, by default.Output
Instead, if you see something like this
Output
The expressions within the brackets will be evaluated from left to right and the result of the last evaluation will be assigned to
d
.This is equivalent to the following:
button
andmenu
are undefined and are typically referenced in your example as variables that are used later throughout the script.That is the same as
And the reason for it is for avoiding variable hoisting.
More about variable hoisting:
http://thecomputersarewinning.com/post/a-dangerous-example-of-javascript-hoisting/