Understanding some Javascript code -


this question has answer here:

i came across javascript snippet:

var sum = +((!+[] + !![] + !![] + []) + (!+[] + !![] + !![] + !![] + !![] + !![])); 

this example evaluates 36.

what happening here , what's best way understand/read it?

!! way convert boolean being either true or false. numerical representation true 1 , false it's 0. example:

!![] => true => 1

when convert +[] number goes 0 (when converted boolean it's false), !+[] true. lets convert items:

var sum = +((!+[] + !![] + !![] + []) + (!+[] + !![] + !![] + !![] + !![] + !![]));  var sum = +((true + true + true + []) + (true + true + true + true + true + true));  var sum = +((3 + []) + (6)); 

now note (3 + []) => "3", evals string. string concatenated number 6:

var sum = +("3" + 6); // "36" 

then +("36") turns string number:

var sum = 36; 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -