JS: Object.prototype.valueOf

By Xah Lee. Date: . Last updated: .

obj.valueOf ()

Convert obj to a object type, return it.

[see JS: “this” Binding]

For example, in obj.valueOf (), if type of obj is type object, returns obj itself.

[see JS: Value Types]

It's usually called in this form:


The purpose is to convert a primitive value arg to a object version of arg.

[see JS: Function Call, Apply, Bind]

It has similar behavior as Object(arg), except in the Object(arg) case, if arg is null or undefined, it creates a empty object, while in the valueOf() case, it throws TypeError.

[see JS: Object Constructor]

[see JS: Primitive Value]

Here's a table showing the conversion:

undefinedthrow TypeError
nullthrow TypeError
booleanboolean object
numbernumber object
stringstring object
symbolsymbol object
const obj = {};
console.log ( obj.valueOf() === obj ); // true

const x1 = Object.prototype.valueOf.call ( true );
console.log ( x1 ); // [Boolean: true]
console.log ( typeof x1 === "object" ); // true

const x2 = Object.prototype.valueOf.call ( 3 );
console.log ( x2 ); // [Number: 3]
console.log ( typeof x2 === "object" ); // true

const x3 = Object.prototype.valueOf.call ( "abc" );
console.log ( x3 ); // [String: 'abc']
console.log ( typeof x3 === "object" ); // true

Error example:

console.log ( Object.prototype.valueOf.call ( null )  );
// TypeError: Cannot convert undefined or null to object


ECMAScript 2015 §Fundamental Objects#sec-object.prototype.valueof

Object and Inheritance Topic

  1. JS: Object Overview
  2. JS: Object Type
  3. JS: Determine Type of Object
  4. JS: Object Literal Expression
  5. JS: Prototype and Inheritance
  6. JS: Create Object
  7. JS: Create Object with Parent X
  8. JS: Get/Set Prototype
  9. JS: Prevent Adding Property
  10. JS: Clone, Deep Copy Object/Array
  11. JS: Test Equality of Objects
  12. JS: Object Object
  13. JS: Object.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.