Since JavaScript is a weakly-typed language, values can also be converted between different types automatically when you apply operators to values of different type, and it is called implicit type coercion.
In general the algorithm is as follows:
- If input is already a primitive, do nothing and return it.
- Call input.toString(), if the result is primitive, return it.
- Call input.valueOf(), if the result is primitive, return it.
- If neither input.toString() nor input.valueOf() yields primitive, throw TypeError.
Numeric conversion first calls valueOf (3) with a fallback to toString (2).
String conversion does the opposite: toString (2)
followed by valueOf (3).
+ operator triggers numeric conversion.
so valueOf will be called for blank array, which is blank array only. [].valueOf() // [].
Since valueOf is not primitive it will call [].toString() //""
so 1 + [] will convert to 1 + "".
Since one of the operands is string operator triggers string conversion for 1.
so 1 + "" will convert to "1" + ""
And hence you get the output "1"
+[]gets evaluated to''because the + converts the empty array to a string. So your expression is equivalent to:![]+1+''+falsewhich is equivalent tofalse+1+''+false, once again when converted to a string due to + operator, comes out to1false.