Number.prototype.valueOf()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Number 值的 valueOf() 方法返回该数字的值。

尝试一下

const numObj = new Number(42);
console.log(typeof numObj);
// Expected output: "object"

const num = numObj.valueOf();
console.log(num);
// Expected output: 42

console.log(typeof num);
// Expected output: "number"

语法

js
valueOf()

参数

无。

返回值

一个表示指定 Number 对象的原始值的数字。

描述

该方法通常由 JavaScript 在内部调用,而非在 Web 代码中显式调用。

示例

使用 valueOf

js
const numObj = new Number(10);
console.log(typeof numObj); // object

const num = numObj.valueOf();
console.log(num); // 10
console.log(typeof num); // number

规范

Specification
ECMAScript® 2025 Language Specification
# sec-number.prototype.valueof

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
valueOf

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见