Javascript-Data Type

Source: Internet
Author: User

Ecmascript has five basic data types: undefined, null, Boolean, number, string, and a complex type of object.

1. undefined

The undefined type has only one value, that is, the special undefined

2. null

The null type has only one value, that is, a special null.

If the defined variable is ready to be used to save the object, it is better to initialize the variable to null, so that the variable stores the reference of an object.

Note: The equal operator (=) between null and undefined always returns true.

3. Boolean

The boolean type has only two values: true and false.

To convert other types to boolean, call the transformation function Boolean (), convert any non-zero number, and convert any object except null to true, 0, Nan, null, and undefined to false.

View code

var m = "hello";var mAsBoolean = Boolean(m);var m1 = 2000;var m1AsBoolean = Boolean(m1);var m2 = new Object();var m2AsBoolean = Boolean(m2);var m3AsBoolean = Boolean(null);var m4AsBoolean = Boolean(0);var m5AsBoolean = Boolean(NaN);var m6AsBoolean = Boolean(undefined);alert("mAsBoolean = Boolean(m):" + mAsBoolean + "\n\r" +      "m1AsBoolean= Boolean(m1) :" + m1AsBoolean + "\n\r" +      "m2AsBoolean= Boolean(m2):" + m2AsBoolean + "\n\r" +      "m3AsBoolean= Boolean(null):" + m3AsBoolean + "\n\r" +      "m4AsBoolean= Boolean(0):" + m4AsBoolean + "\n\r" +      "m5AsBoolean= Boolean(NaN) :" + m5AsBoolean + "\n\r" +      "m6AsBoolean= Boolean(undefined):" + m6AsBoolean);

View results

4. Number

In ecmascript, integers and floating-point numbers are expressed in ieee754 format. Because the number () function is complicated to convert strings and is not reasonable enough, parseint () and parsefloat () are generally used ()

View code

 var octnum = "070";    var hexnum = "0xa";    var intnum = 56;    var floatNum = 1.389;    var str = "hello";    var str1 = "123hi";    var str2 = "hello456";    var str3 = "test789hi";    alert("Number(octnum):" + Number(octnum) + "," + 'parseInt(octnum,8):' + parseInt(octnum, 8) + "," + 'parseInt(octnum)' +parseInt(octnum)+"\n\r" +        "Number(hexnum):" + Number(hexnum) + "," + 'parseInt(hexnum,16):' + parseInt(hexnum, 16) + "," + 'parseInt(hexnum)' +parseInt(hexnum)+"\n\r" +        "Number(intnum):" + Number(intnum) + "," + 'parseInt(intnum,10):' + parseInt(intnum, 10) + "," + 'parseInt(intnum)' +parseInt(intnum) +"\n\r" +        "Number(floatNum):" + Number(floatNum) + "," + 'parseFloat(floatNum):' + parseFloat(floatNum) + "\n\r" +        "Number(str):" + Number(str) + "," + 'parseInt(str):' + parseInt(str) + "," + 'parseFloat(str)' + parseFloat(str) + "\n\r" +        "Number(str1):" + Number(str1) + "," + 'parseInt(str1):' + parseInt(str1) + "," + 'parseFloat(str1)' +parseFloat(str1)+ "\n\r" +        "Number(str2):" + Number(str2) + "," + 'parseInt(str2):' + parseInt(str2) + "," + 'parseFloat(str2)' + parseFloat(str2)+"\n\r" +        "Number(str3):" + Number(str3) + "," + 'parseInt(str3):' + parseInt(str3) + "," + 'parseFloat(str3)' + parseFloat(str3));

View results

5. String

String is a character sequence consisting of zero or multiple 16-bit Unicode characters. A string can be expressed by double quotation marks or single quotation marks.

There are two methods to convert a value to a string:

1 indicates that each value has a tostring () method. By default, the tostring () method returns a numeric string in a 10-digit format, and can also pass the base number.

For example: var num = 100; num. tostring (16 );

2. If you do not know whether the type to be converted is null or undefined, you can use string ().

String () follows the conversion rule: if the value has the tostring () method, tostring () is called. If the value is null, "null" is returned. If the value is undefined, then return undefined

6. The object is complex. I will summarize it separately.

 

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.