Conversion of Data Types in javascript

Source: Internet
Author: User

The conversion rules for converting to a Boolean value in javascript are as follows:
1. The special values undefined and null are changed to false.
2. The numbers 0 and NaN change to false.
3. Convert the null string to false.
4. All other values are changed to true.
Where
The undefined value has three conditions:
1. You declared a variable but did not assign a value to it. For example,

<Script>
Var;
Alert ();
</Script>
<Script>
Var;
Alert ();
</Script>
2. You have accessed an attribute that has not been declared by the object (in javascript, You can regard everything as an attribute of the object), for example:

<Script>
Var test = new Object ();
Test. a = "apple ";
Test. B = "orange ";
Alert (test. c );
</Script>
<Script>
Var test = new Object ();
Test. a = "apple ";
Test. B = "orange ";
Alert (test. c );
</Script>
Test. c is not declared.
3. You have defined the function parameter but have not passed the value to it. For example:

<Script>
Function test (ob ){
Alert (ob );
}
Test ();
</Script>
<Script>
Function test (ob ){
Alert (ob );
}
Test ();
</Script>
NaN (non-numeric value) is generated when the value cannot be converted into a number, for example:

<Script>
Var string = "I am a student ";
Var number = 42;
Alert (string * number );
</Script>
<Script>
Var string = "I am a student ";
Var number = 42;
Alert (string * number );
</Script>

One of the elegant features of javascript is that it is easy to convert a value from one data type to another. For example:

<Script>
Var a = 4;
Var B = '3 ';
Var c = a * B;
Alert (c );
</Script> www.2cto.com
<Script>
Var a = 4;
Var B = '3 ';
Var c = a * B;
Alert (c );
</Script> the preceding Code triggers data type conversion. The multiplication operator requires a number, so javascript tries to convert it to a number as much as possible. In the preceding example, It is easy: B is interpreted as number 3, so c is changed to 12.

From the cangkukuaimanle Column
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.