JavaScript Number valueOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

number.valueOf() returns the primitive number value. On a plain number literal it returns the same value. On a Number object created with new Number(...), it unwraps the object to a real number.

You rarely call it yourself. JavaScript uses it during math and many conversions. Prefer number literals over new Number() in everyday code.

Syntax

Call valueOf with no arguments.

JavaScript
n.valueOf()
CallWhat it does
n.valueOf()Return the primitive number for n.
(new Number(x)).valueOf()Unwrap a Number object to the number x.

Built into JavaScript. Call valueOf() on a number (or Number instance) — no import required.

Library declarations
JavaScript
Number.prototype.valueOf()

Parameters

ParameterDescription
—None. Call valueOf() with empty parentheses.

Return Value

Return ValueDescription
numberThe primitive numeric value (typeof is "number").

JavaScript Examples

Example 1 — Unwrap a Number Object

Compare a primitive with a Number object, then call valueOf().

JavaScript
const primitive = 42;
const boxed = new Number(42);

console.log(typeof primitive);        // "number"
console.log(typeof boxed);            // "object"
console.log(boxed.valueOf());         // 42
console.log(typeof boxed.valueOf());  // "number"
Try It Yourself

How It Works

  1. Literal. 42 is a primitive with typeof "number".
  2. Wrapper. new Number(42) is an object.
  3. Unwrap. valueOf() returns the stored primitive 42.
  4. Type. After unwrapping, typeof is "number" again.

Example 2 — Loose vs Strict Equality

See why == and === disagree for Number objects.

JavaScript
const boxed = new Number(10);

console.log(boxed == 10);                 // true  (valueOf used)
console.log(boxed === 10);                // false (object !== number)
console.log(boxed.valueOf() === 10);      // true
console.log(Object.is(boxed.valueOf(), 10)); // true
Try It Yourself

How It Works

  1. Loose equality. == converts the object using valueOf(), so it matches 10.
  2. Strict equality. === requires the same type, so object vs number is false.
  3. Explicit unwrap. Compare boxed.valueOf() when you need a real number.
  4. Object.is. Same idea for same-value comparison after unwrapping.

Example 3 — Automatic Conversion in Math

Math operators call valueOf() for you.

JavaScript
const boxed = new Number(7);

console.log(boxed + 3);           // 10
console.log(boxed * 2);           // 14
console.log(+boxed);              // 7  (unary plus)
console.log(String(boxed));       // "7" (uses toString)
console.log(typeof (boxed + 0));  // "number"
Try It Yourself

How It Works

  1. Addition / multiply. The engine unwraps the object, then does math.
  2. Unary plus. +boxed is a short way to get the primitive number.
  3. String(). Prefers string conversion via toString().
  4. Result type. Math results are primitives, not Number objects.

Browser & Runtime Support

Number.prototype.valueOf() is part of core JavaScript (ES1+) and is Baseline Widely available.

✓ Baseline · ES1+

Number.valueOf()

Supported in every browser and Node.js version. No polyfill needed.

99% Universal 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
valueOf Excellent

Bottom line: Safe everywhere. Prefer number literals; call valueOf() when you need to unwrap a Number object on purpose.

Frequently Asked Questions

Number.prototype.valueOf() returns the primitive number stored in a Number object. Example: (new Number(42)).valueOf() is 42 with typeof "number".
Usually no. JavaScript calls valueOf() automatically when a Number object is used in math or most comparisons. You mainly call it when teaching, debugging, or unwrapping a Number wrapper on purpose.
A literal like 42 is a primitive (typeof "number"). new Number(42) is an object (typeof "object"). Prefer primitives in everyday code. valueOf() unwraps the object back to the primitive.
valueOf() returns a number. toString() returns a string. Both can be used by JavaScript during type conversion, but they produce different types.
No. Call it with empty parentheses: n.valueOf().
It is part of early ECMAScript and is Baseline Widely available — safe in every modern browser and Node.js.

Next: Number Properties

Browse EPSILON, safe integers, Infinity, and NaN.

Number Properties →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful