JavaScript valueOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Object

Definition

obj.valueOf() returns the object’s primitive value when it has one. For a plain object, it usually returns the object itself.

Built-in wrappers and types override it: Number and Boolean return their primitives, and Date.valueOf() returns a timestamp number. JavaScript may call valueOf during numeric operations.

Syntax

Call obj.valueOf() like this. Start with this line.

JavaScript
obj.valueOf()
CallWhat it does
obj.valueOf()Returns a primitive when overridden; otherwise often the object itself.
({}).valueOf() === ({})Each new object is a different reference — compare carefully.
new Date("2026-01-15").valueOf()Returns the date as milliseconds since epoch.

No parameters. Plain objects typically get back the same object reference.

Library declarations
JavaScript
obj.valueOf()
JavaScript
object.valueOf(): Object

Parameters

ParameterDescription
(none)Object.prototype.valueOf takes no parameters.

Return Value

Return ValueDescription
anyA primitive value if the type provides one; for plain objects, usually the object itself.

JavaScript Examples

Plain Objects Return Themselves

Call valueOf on a normal object and compare references.

JavaScript
var user = { name: "Ada" };
var same = user.valueOf();

console.log(same);
console.log(same === user);
console.log(typeof same);
Try It Yourself

How It Works

  1. Same object. valueOf returns the object itself for plain objects.
  2. Reference match. same === user is true.
  3. Still an object. typeof stays "object".

Date valueOf Returns a Timestamp

Dates override valueOf with a number.

JavaScript
var day = new Date(Date.UTC(2026, 0, 15));

console.log(day.valueOf());
console.log(typeof day.valueOf());
console.log(day.valueOf() === day.getTime());
Try It Yourself

How It Works

  1. Number result. Date.valueOf() is milliseconds since Jan 1, 1970 UTC.
  2. Primitive type. typeof is "number", not "object".
  3. Same as getTime. It matches getTime() for the same date.

Override valueOf for Numeric Objects

Make a small score object that acts like a number in math.

JavaScript
var score = {
  points: 40,
  bonus: 5,
  valueOf: function () {
    return this.points + this.bonus;
  }
};

console.log(score.valueOf());
console.log(score + 10);
console.log(Number(score));
Try It Yourself

How It Works

  1. Custom primitive. Your valueOf returns the total 45.
  2. Math uses it. Adding 10 becomes 55 because JavaScript asks for a primitive.
  3. Number() too. Number(score) also goes through the primitive conversion path.

Browser & Runtime Support

valueOf() has been in JavaScript since ES1 and is Baseline across all browsers and runtimes.

✓ ES1 · Baseline

obj.valueOf()

Universal support. Most useful when types override it (Date, Number wrappers) or when you define your own.

100% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Object.prototype.valueOf Excellent

Bottom line: Safe everywhere. For plain objects it usually returns the object itself; override when you need primitive behavior.

Frequently Asked Questions

Usually the object itself. It does not turn the object into a string or number unless you override it.
During primitive conversion for math and some comparisons. Display text more often uses toString.
valueOf aims for a primitive value (often for math). toString aims for text.
A number: milliseconds since the Unix epoch, same idea as getTime().
Only when your object should behave like a number or other primitive in expressions. For labels, override toString instead.

Next: Object Methods

Browse every Object method tutorial.

Object Methods tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful