以下是示例布尔值valueOf()方法。
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:
这里的值lsbin会给出错误, 因为不仅未定义该值, 而且还预定义了false。
<script>
// Here Boolean object obj is created
// for the value lsbin.
var obj = new Boolean(lsbin);
// Here boolean.valueOf() function is
// used for the created object obj.
console.log(obj.valueOf());
</script>
输出如下:
Error: lsbin 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浏览器
火狐浏览器
苹果浏览器
歌剧