Need assistance in understanding javascript order of precedence

Issue

I’m having an issue understanding how the following statement is working.

// setup
var n = 3;
var J = 3;
var g = 0;
var p = 41;
var m = false;
var O = 22;
var z = 15;
var I = [7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,21,19,20,22,18,20,19,21,7,7,17,17,17,17,17,17,17,17,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,25,25,25,25,25,25,25,25,7,7,29,27,28,30,26,28,27,29,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,,,22]

// statement
n += J || (g = p, m = p < O ? g - 3 : g + 2, I[m] < z | I[m + O - p] || I[p += p - O]) ? 1 : 0;

At the end of the statement n=4 but I don’t understand how or why. I would expect:

n += 3 || 1 // n = 6

This is someone else’s code I’m trying to understand. Any help is appreciated.

Solution

It’s quite simple.

J || (g = p, m = p < O ? g – 3 : g + 2, I[m] < z | I[m + O – p] || I[p += p – O]) ? 1 : 0

This above equals (J || anything) ? 1 : 0. Since J=3, it’s like (3 || anything) ? 1 : 0.

n += 3 || anything ? 1 : 0 -> n += 1

Answered By – Giovanni Londero

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published