javascript---boolean type conversions

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.

 

你可能感兴趣的:(JavaScript)