Boolean.prototype.toString()
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.
toString()
方法返回表示指定的布尔对象的字符串。
尝试一下
const flag1 = new Boolean(true);
console.log(flag1.toString());
// Expected output: "true"
const flag2 = new Boolean(1);
console.log(flag2.toString());
// Expected output: "true"
语法
js
toString()
返回值
一个表示特定 Boolean
对象的字符串。
描述
Boolean
对象覆盖了 Object
对象的 toString
方法。并没有继承 Object.prototype.toString()
。对于布尔对象,toString
方法返回一个表示该对象的字符串。
当一个 Boolean
对象作为文本值或进行字符串连接时,JavaScript 会自动调用其 toString
方法。
对于 Boolean
对象或值,内置的 toString
方法返回字符串 "true"
或 "false"
,具体返回哪个取决于布尔对象的值。
示例
使用 toString()
下面的代码,flag.toString()
返回 "true"
:
js
const flag = new Boolean(true);
const myVar = flag.toString();
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-boolean.prototype.tostring |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.