javascript---boolean type conversions

本文介绍了在JavaScript中布尔值与其他类型之间的转换规则,并详细解释了数字0、NaN及空字符串在不同上下文中的布尔值行为。此外,还提供了一个使用isNaN函数检测NaN的示例。

Boolean values are easily convertible to and from other types and are often automatically converted.If a boolean value is used in a numeric context,true converts to number 1 and false converts to the number 0.

     if a boolean value is used in a string context,true converts to string "true" and false converts to string "false" .

     if a number is used as a bool value,the number is converted to true unless the number is 0 or NaN,which are converted to false.

     if a string is used where a bool value is expected ,it is converted to true except for the empty string which is converted to false.

     if a string value is null or undefined value ,which are also converted to false;

     any non-null object,array,or function all convert to true value as a boolean,or convert to false ;

     Oh,something about NaN,in javascript when a mathematical operation returns a undefined result or an error.in this case ,the result is printed ad NaN(such as divsion of 0 by 0).

     the NaN is so sepecial that it is equal to nothing ,even to itself.Now,how do we know there is an NaN,don't worry,Js provides a special function,isNaN,which is used to test whether a number is an NaN or not,just like as follows:

 

function testIsNaN()
{
var result=parseInt("Alex");
if(isNaN(result))
{
alert(
"error");
}
else
{
alert(
"ok');
}
}

 

as we know ,this result is a NaN,so .........

 

my god ,this is my first english blog,maybe there are some grammar errors and some vocabulary errors,all are not important for me ,because I have taken the first step.I don't feel embarrassed when I make a mistake.more interesting and exiting things will be here in the following days.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值