Boolean.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.

Boolean 值的 valueOf() 方法返回 Boolean 对象的原始值。

尝试一下

const x = new Boolean();

console.log(x.valueOf());
// Expected output: false

const y = new Boolean("Mozilla");

console.log(y.valueOf());
// Expected output: true

语法

js
valueOf()

参数

无。

返回值

给定 Boolean 对象的原始值。

描述

BooleanvalueOf() 方法以布尔数据类型返回 Boolean 对象或 Boolean 字面量的原始值。

该方法通常在 JavaScript 内部调用,而不是在代码中显式调用。

示例

使用 valueOf()

js
const x = new Boolean();
const myVar = x.valueOf(); // 给 myVar 赋值 false

规范

Specification
ECMAScript® 2025 Language Specification
# sec-boolean.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

参见