1

I see a lot of funny code in Javascript but i can not understand, Why this expression is equal to fail?

document.write((![] + [])[+[]] + (![] + [])[+!+[]] + ([![]] + [][[]])[+!+[] + [+[]]] + (![] + [])[!+[] + !+[]]);    // -> 'fail'

Edit 1:

This is not duplicate of javascript gotcha: empty list to boolean conversion .. i saw it and i google it before ask, but there's something new here, I cannot find why fail come from this expression.

Mehdi Yeganeh
  • 2,019
  • 2
  • 24
  • 42
  • @Bergi, I see your link before and possible we could get these letters `f`, `a`, `l` from toString of "false" but for the letter `i` can not understand comes from? – Mehdi Yeganeh Aug 23 '19 at 17:51
  • 1
    @MehdiYeganeh from `undefined`, in this code it's actually `falseundefined` because `i` is the 10th letter and it's easy to construct the number 10 – Wendelin Aug 23 '19 at 17:58
  • 1
    The reason it evaluates to a string is because of two things, 1. `[]` not is not only an empty array but also the syntax for indexing strings and arrays `arr[index]`, 2. javascript will always attempt to coerce types rather than crash so something like `+[]` will evaluate to `0` and `!+[]` to `1`. See this fiddle for a breakdown of the entire string: https://jsfiddle.net/olian04/6v91qmn3/42/ – Olian04 Aug 23 '19 at 18:09
  • @Wendelin, @Olian04, Appreciate for your comments. I found my answer. It's from `undefined`. – Mehdi Yeganeh Aug 23 '19 at 19:29

0 Answers0