toString() 方法返回表示指定的布尔对象的字符串。

{{InteractiveExample("JavaScript Demo: Boolean.toString()")}}

```js interactive-example 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-nolint toString()

返回值

一个表示特定 Boolean 对象的字符串。

描述

Boolean 对象覆盖了 Object 对象的 toString 方法。并没有继承 Object.prototype.toString()。对于布尔对象,toString 方法返回一个表示该对象的字符串。

当一个 Boolean 对象作为文本值或进行字符串连接时,JavaScript 会自动调用其 toString 方法。

对于 Boolean 对象或值,内置的 toString 方法返回字符串 "true" 或 "false",具体返回哪个取决于布尔对象的值。

示例

使用 toString()

下面的代码,flag.toString() 返回 "true":

const flag = new Boolean(true);
const myVar = flag.toString();