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

valueOf() 方法返回 String 对象的字符串值。

尝试一下

const stringObj = new String("foo");

console.log(stringObj);
// Expected output: String { "foo" }

console.log(stringObj.valueOf());
// Expected output: "foo"

语法

js
valueOf()

返回值

一个字符串,表示给定 String 对象的原始值。

描述

StringvalueOf() 方法以字符串数据类型返回 String 对象的原始值。此值等价于 String.prototype.toString()

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

示例

使用 valueOf()

js
const x = new String("Hello world");
console.log(x.valueOf()); // 'Hello world'

规范

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

参见