+true // result: 1
true.valueOf() // result: true
+true === true.valueOf() // result: false
In Javascript Type Coersion, the function called for evaluation is valueOf(). But if the function is called explicity it returns a different value.
the function called for evaluation is valueOf()
Not always. valueOf()
is only meaningful for non-primitive types, since it is defined to return the primitive value of the given object. true
by itself is a Boolean primitive and as such calling true.valueOf()
would be completely redundant.
The unary +
and -
sign operators always return a number by definition. Since a Boolean quite conveniently converts to a number, it only makes sense that +true
returns 1.
There is no reason +true
and true.valueOf()
should both correspond to the same value.