以下是布尔valueOf()方法的示例。
- 例:
<script> // Here Boolean object obj // is created for the value 27 var obj = new Boolean(27); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf());
- 输出:
true
boolean.valueOf()方法用于根据指定的布尔对象的值返回布尔值“true”或“false”。
用法:
boolean.valueOf()
参数:此方法不接受任何参数。
返回值:它根据指定的布尔对象的值返回布尔值“true”或“false”。
上述方法的更多代码如下:
- 程序1:
<script> // Here Boolean object obj is created // for the value true. var obj = new Boolean(true); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script>
输出:
true
- 程序2:
<script> // Here Boolean object obj is // created for the value 1. var obj = new Boolean(1); // Here boolean.valueOf() function // is used for the created object obj. document.write(obj.valueOf()); </script>
输出:
true
- 程序3:
<script> // Here Boolean object obj is // created for the value -1. var obj = new Boolean(-1); // Here boolean.valueOf() function // is used for the created object obj. document.write(obj.valueOf()); </script>
输出:
true
- 程序4:
<script> // Here Boolean object obj is // created for the value 1.2 var obj = new Boolean(1.2); // Here boolean.valueOf() function // is used for the created object obj. document.write(obj.valueOf()); </script>
输出:
true
- 程序5:
<script> // Here Boolean object obj is // created for the value as string "gfg" var obj = new Boolean("gfg"); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script>
输出:
true
- 程序6:
<script> // Here Boolean object obj is created for the value false. var obj = new Boolean(false); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script>
输出:
false
- 程序7:
<script> // Here Boolean object obj is created // for the value zero (0) var obj = new Boolean(0); // Here boolean.valueOf() function is // used for the created object obj. document.write(obj.valueOf()); </script>
输出:
false
- 程序1:这里的值geeksforgeeks会产生错误,因为不仅未定义该值,而且还预定义了false。
<script> // Here Boolean object obj is created // for the value geeksforgeeks. var obj = new Boolean(geeksforgeeks); // Here boolean.valueOf() function is // used for the created object obj. console.log(obj.valueOf()); </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.valueOf() function is // used for the created object obj. console.log(obj.valueOf());
输出:
Error:Invalid or unexpected token
错误和异常:在控制台上检查此程序。
支持的浏览器:下面列出了JavaScript布尔valueOf()方法支持的浏览器:
- 谷歌浏览器
- IE浏览器
- 火狐浏览器
- Safari
- Opera
相关用法
- JavaScript Number valueOf()用法及代码示例
- JavaScript Date valueOf()用法及代码示例
- Javascript Array valueOf()用法及代码示例
- Javascript string.valueOf()用法及代码示例
- JavaScript BigInt.prototype.valueOf()用法及代码示例
- Javascript symbol.valueOf()用法及代码示例
- Typescript String valueOf()用法及代码示例
- Typescript Number valueOf()用法及代码示例
- Lodash _.prototype.valueOf()用法及代码示例
- Moment.js moment().valueOf()用法及代码示例
- JavaScript Boolean toString()用法及代码示例
- javascript Boolean constructor用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript Boolean valueOf() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。