Understand valueOf() in JavaScript

The valueOf() function is a special function on all JavaScript objects. According to the Mozilla docs, the valueOf() function "returns the primitive value of the specified object." There are 6 primitive types in JavaScript:

Boolean: true and false

and Number: -1 , 0 , 3.14 , etc.

, , , etc. String: 'foo' '

' Null

Undefined

Symbol: Symbol('mySymbol')

The valueOf() function should return a value whose typeof is one of the above types. Here's what valueOf() returns for 3 common JavaScript classes: String , Number , and Date . String#valueOf() and Number#valueOf() "unbox" the object and give you the corresponding primitive string or number. Date#valueOf() gives you the JavaScript timestamp.

const s = new String ( 'test' ); typeof s; s.valueOf(); typeof s.valueOf(); const n = new Number ( 42 ); n.valueOf(); const d = new Date ( '2019-06-01' ); d.valueOf();

JavaScript uses the valueOf() function for type coercion when comparing values using the operators == (as opposed to === ), < , > , <= , and >= . For example, here is the part of the JavaScript language specification that discusses using valueOf() for the < operator.

Suppose you have an object whose valueOf() always returns 0. As far as the < and == operators are concerned, this object is equivalent to the primitive number 0. However, for == , this object is not equivalent to new Number(0) , because JavaScript does not call valueOf() when using == to compare two objects.

class MyClass { valueOf() { return 0 ; } } const obj = new MyClass(); obj < new Number ( -1 ); obj > new Number ( -1 ); obj < -1 ; obj > -1 ; obj == new Number ( 0 ); obj == 0 ; 0 == obj; obj === Number ( 0 );

More Fundamentals Tutorials