以下是布尔toString()方法的示例。
- 例:
<script> // Here Boolean object obj // is created for the value 27 var obj = new Boolean(27); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
- 输出:
true
boolean.toString()方法用于根据指定的布尔对象的值返回“true”或“false”字符串。
用法:
boolean.toString()
参数:此方法不接受任何参数。
返回值:根据指定的布尔对象的值,它返回一个字符串“true”或“false”。
上述方法的更多代码如下:
- 范例1:
<script> // Here Boolean object obj is // created for the value true. var obj = new Boolean(true); // Here boolean.toString() function // is used for the created object obj. document.write(obj.toString()); </script>
输出:
true
- 范例2:
<script> // Here Boolean object obj is // created for the value 1. var obj = new Boolean(1); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
输出:
true
- 范例3:
<script> // Here Boolean object obj is // created for the value -1. var obj = new Boolean(-1); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
输出:
true
- 范例4:
<script> // Here Boolean object obj is // created for the value 1.2 var obj = new Boolean(1.2); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
输出:
true
- 范例5:
<script> // Here Boolean object obj is created // for the value as string "gfg" var obj = new Boolean("gfg"); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
输出:
true
- 范例6:
<script> // Here Boolean object obj is created // for the value false. var obj = new Boolean(false); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
输出:
false
错误和异常:
- 范例1:这里的值geeksforgeeks会给出错误,因为不仅未定义该值,而且还预定义了false。
<script> // Here Boolean object obj is created // for the value geeksforgeeks. var obj = new Boolean(geeksforgeeks); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); </script>
输出:
Error:geeksforgeeks is not defined
- 范例2:此处不能将复数作为参数,而只能将整数值和字符串作为参数,这就是它返回错误的原因。
<script> // Here Boolean object obj is created for the // value such as complex number 1+2i var obj = new Boolean(1 + 2i); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); </script>
输出:
Error:Invalid or unexpected token
支持的浏览器:下面列出了JavaScript布尔toString()方法支持的浏览器:
- 谷歌浏览器
- IE浏览器
- 火狐浏览器
- Safari
- Opera
相关用法
- Javascript RegExp toString()用法及代码示例
- Javascript toString()用法及代码示例
- JavaScript Array toString()用法及代码示例
- Javascript date.toString()用法及代码示例
- Javascript string.toString()用法及代码示例
- JavaScript BigInt.prototype.toString()用法及代码示例
- JavaScript Error.prototype.toString()用法及代码示例
- JavaScript Boolean valueOf()用法及代码示例
- Javascript typedArray.toString()用法及代码示例
- Javascript symbol.toString()用法及代码示例
- javascript Boolean constructor用法及代码示例
- Node.js Buffer.toString()用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript Boolean toString() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。