Wednesday, 15 May 2013

What is !+[]+!+[] in javascript? -



What is !+[]+!+[] in javascript? -

hi reading article , found pretty unusual results below code in javascript homecoming 2.

!+[]+!+[]

can please explain.

breaking downwards look right order of operations, have:

(!(+[])) + (!(+[]))

first things first, [] cast number +, results in 0. don't inquire me why, :p buried in specification somewhere.

!0 true

so end true + true, 1 time again casts numbers, resulting in 1 + 1 = 2

to nine, you'd need 9 repetitions:

!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[] == 9

javascript

No comments:

Post a Comment