Hacker News new | ask | show | jobs
by ahoge 3923 days ago
In case anyone wonders about the `!+[1,]` bit:

In modern browsers, `[1,]` is the same as `[1]`. If you coerce it to number, you get 1. (You get "1" if you toString it.)

In IE8, `[1,]` is the same as creating an array with a length of 2 whose first slot contains 1 and whose second slot was left empty. It's kinda like `[1, undefined]`, but there is no "1" key. If you coerce it to number, you get NaN. (You get "1," if you toString it.)

If you coerce 1 to boolean and negate it, you get false.

If you coerce NaN to boolean and negate it, you get true.

So, basically, it just checks if trailing commas in array literals are handled incorrectly.

1 comments

Interesting explanation. When I last checked that the "!+[1,]" expression alone was coherent across all the browser but "if(!+[1,])" statement was not.

I think there is something weird about the parsing of the comma in "if" statement in old IE.

Correction. There is nothing special in "if". IE9 is just different, as you explained.