此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
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 2015年7月.
valueOf() 方法返回String 对象的字符串值。
In this article
尝试一下
const stringObj = new String("foo");console.log(stringObj);// Expected output: String { "foo" }console.log(stringObj.valueOf());// Expected output: "foo"语法
js
valueOf()返回值
一个字符串,表示给定String 对象的原始值。
描述
String 的valueOf() 方法以字符串数据类型返回String 对象的原始值。此值等价于String.prototype.toString()。
此方法通常由 JavaScript 在内部调用,而不是在代码中显式调用。
示例
>使用valueOf()
js
const x = new String("Hello world");console.log(x.valueOf()); // 'Hello world'规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-string.prototype.valueof> |