-1
+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.

Vijay
  • 11
  • 1
  • 4
    I don't understand. `+true` coerces a boolean to a number. `true.valueOf()` returns `true`. Of course `1` does not **strictly equal** `true`. – deceze Nov 28 '14 at 13:42
  • You are comparing boolean and integer values and wondering why that is not the same? What was the question? – feeela Nov 28 '14 at 13:42
  • Why true.valueOf() doesn't returns 1 – Vijay Nov 28 '14 at 13:45
  • 1
    Why would it? `.valueOf()` is not defined to return numbers. It returns the primitive value of an object. In case of a bool, it returns a bool. – deceze Nov 28 '14 at 13:46
  • possible duplicate of [Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – WBAR Nov 28 '14 at 13:49
  • I was reading a book Expert Javascript. I didn't understand the line, "Without specifying a hint for the internal DefaultValue function, JavaScript assumes you want a number. This results in a call to valueOf() instead". So my assumption was .valueOf() would return a numeric value. – Vijay Nov 28 '14 at 13:56

3 Answers3

3

Type Coersion in Javascript happens if == is used, which is kinda loose comparison operator.

=== is strict comparison operator which doesn't coerce the types when comparing so it remains an integer and the other one bool

+true === true.valeOf() // false
+true == true.valueOf() // true

Docs:

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

Why true.valueOf() doesn't returns 1

The answer is true.valueOf returns true, which is the primitive value of a Boolean object. Also the quote is from MDN

The valueOf method of Boolean returns the primitive value of a Boolean object or literal Boolean as a Boolean data type.

What does +true do:

+true is same as Number(true) and it is a well known fact that 0 is false and 1 is true in almost every language. In fact in C++ they are used as booleans.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • 2
    @WBAR What? An instance of X can be the value of Y. What did you mean? – feeela Nov 28 '14 at 13:44
  • I think the OP understands how `===` works and is asking why `+true` doesn't return the exact same thing as `true.valueOf()`. – BoltClock Nov 28 '14 at 13:48
  • Type coercion, like forcing a value to a primitive type, does happen in many places, not only in `==`??? – Bergi Nov 28 '14 at 13:57
  • @Bergi I'm not that deep into the language, so didn't know that. From what I learnt answering here, and from the context, I made that statement. Edited. Thanks – Amit Joki Nov 28 '14 at 13:59
0

for strict comparison you should do like this:

Number(true.valueOf()) === +true
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356