Tell me, please, as a unit?

+!{}[0] // =1 

Thank!

  • 6
    {}[0] -> undefined -> true , +true -> 1 - Grundy
  • @Grundy, and I didn’t immediately fuck up - Yuri
  • 2
    @Grundy, add an answer not a comment, but a response. Please :) - Yuri
  • one
    Do not like the object - take the array: +![][[]] - vp_arth
  • one
    @Alex, This refers to access to the properties of an object through square brackets — and the properties of an array and properties of an object can be accessed as via , and through [] . The main difference is that when using a dot, the property name must be a valid js identifier, and this imposes some restrictions, such as numbers, are not identifiers, so they cannot be used with a dot. On the other hand, the following entries are equivalent and the variable in them can be both an array and an object: a['toString']() , a.toString() - Grundy

1 answer 1

tl; dr; {}[0]undefined !undefined → true, +true → 1


Bracket notation

The first thing that is used in this expression: Bracket notation - the ability to access the properties of objects using square brackets and a string key.

{}[0] - an attempt to take a property with the name 0 in an empty object. Since there is no such property, a regular undefined result will be obtained.


logical negation operator ( ! )

Further, the logical negation operator ( ! ) Is applied to the result ( undefined ). This operator returns applies the abstract method ToBoolean to the operand, and inverts the result.

As you can see from the table, for undefined the result is ToBoolean(undefined)false , after inverting we get true .


unary operator +

Further, a unary operator + is applied to the result ( true ), which simply translates the result into a number. To do this, use the abstract function ToNumber.

As you can see from the table, for true, the result is ToNumber(true)1 .