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

JS Object and Inheritance

  1. Object Overview
  2. Object Type
  3. Find Object's Type
  4. Prototype Chain
  5. Create Object
  6. Object Literal Expr
  7. Create Object with Parent X
  8. Get/Set Parent
  9. Show Prototype Chain
  10. Prevent Adding Property
  11. Clone Object
  12. Test Object Equality
  13. Add Method to Prototype

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

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

Web Dev Tutorials