Boolean.valueOf()是javascript中的内置函数,用于根据指定的布尔对象的值返回布尔值“true”或“false”。
如果字符串参数为null,则返回false,否则返回true。
用法:
boolean.valueOf()
参数:这个函数不接受任何参数。
返回值:它根据指定的布尔对象的值返回布尔值“true”或“false”。
让我们看一下此boolean.toString()函数上的一些JavaScript代码:
- 示例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
错误和异常:在控制台上查看此示例。
应用:
此函数的应用程序将根据给定对象的值返回布尔值“true”或“false”。
让我们看看此应用程序上的JavaScript程序:
- 示例1:
<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
相关用法
- Javascript Math.pow( )用法及代码示例
- Javascript Array some()用法及代码示例
- Javascript Number()用法及代码示例
- Javascript Symbol.for()用法及代码示例
- Javascript toExponential()用法及代码示例
- Javascript toString()用法及代码示例
- Javascript Math.abs( )用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript | boolean.valueOf() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。