In JavaScript, when you put multiple pairs inside a pair of brackets, they are evaluated as the last expression, as in the example below:
var a = (1, 2); var b = a + 1;
So, in your case, the interpreter performs the attribution n = "value" , and then analyzes if it accepts the condition a == b as a condition. This is the same as:
n = "value"; if (a == b) {
This article explains this behavior.
EDIT
However, this does not limit n the if region. The same thing happens with var declarations in for loops:
for (var i = 0; i < 10; i++) {
EDIT 2
As Ethan Brown mentioned, itβs also useful to talk about variable lifting, which basically means that in JavaScript values ββcan be assigned to variables before they are declared. The following code shows this behavior and was extracted from this MDN article :
bla = 2 var bla;
The same thing happens with functions:
foo(); function foo() { console.log('bar'); }
source share