toString() 方法返回指定的布尔对象的字符串形式。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
bool.toString()
表示特定Boolean对象的字符串。
Boolean 对象覆盖了 Object 对象的 toString 方法。并没有继承 Object.prototype.toString()。对于布尔对象,toString 方法返回该对象的字符串形式。
当一个Boolean对象作为文本值或进行字符串连接时,JavaScript 会自动调用其 toString 方法。
对于Boolean对象或值,内置的 toString 方法返回字符串 "true" 或 "false",具体返回哪个取决于布尔对象的值。
toString下面的代码,flag.toString 返回 "true":
var flag = new Boolean(true)
var myVar = flag.toString()
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) Boolean.prototype.toString |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) Boolean.prototype.toString |
Standard | |
| ECMAScript Latest Draft (ECMA-262) Boolean.prototype.toString |
Draft |
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString | Chrome Full support Yes | Edge Full support 12 | Firefox Full support 1 | IE Full support Yes | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |