JavaScript data types

Source: Internet
Author: User

There are 5 simple data types in ECMAscript, also known as basic data types: Undefined, Null, Boolean, number, and string. There is also a complex data type of--object.

Undefined type

When you declare a variable with VAR but do not initialize it, the value of this variable is undefined. Such as:

?
12 varnumber;document.write(number); //undefined

If you do not declare a variable, the following error occurs. Such as:

?
1 document.write(str); //错误

But when executed with typeof, the undefined value is returned regardless of whether or not a declaration is made. Such as:

?
123 varnum;document.write(typeof num); //undefineddocument.write(typeofstr); //undefined

Null type

When NULL is detected using typeof, "Object" is returned. Such as:

?
12 varnum = null;document.write(typeofnum); //object

The undefined value is actually derived from a null value, so they will return true if they are equal. Such as:

?
1 alert(null== undefined); //true

It is important to note that, as long as the variable that is intended to save the object does not actually hold the object, it should be explicitly saved with a null value, which helps to distinguish between null and undefined. Such as:

?
123 varnum1 = null;var num2;document.write(typeof num1 + " " + typeofnum2); //object undefined

Boolean type

To convert a value to its corresponding Boolean value, you can use the transform function, Boolean (). Such as:

?
12 varstr = "helloworld";document.write(Boolean(str)); //true

The 3.4.4 in the book "JavaScript Advanced Programming Design" gives a table of conversion rules for various data types and their corresponding. There are probably the following types:

    • When the data type is string, any non-empty strings are converted to true;
    • When the data type is number, all but 0 and nan are converted to false, and the others are converted to true;
    • When the data type is object, all but null are converted to true;
    • When the data type is undefined, n/A will be converted to true,undefined to be converted to false;

It is important to note that the IF statement often uses the Boolean conversion of the auto-execute response as a condition. Such as:

?
1234 varstr = "helloworld";if(str){  document.write("hellothere");} //hellothere

Number Type

In addition to being decimal, integers can be represented by octal or hexadecimal literals, where the first digit of the octal literal must be 0, followed by the octal sequence (0~7). Such as:

?
1234 varnum1 = 070; //八进制的56var num2 = 032; //八进制的32var num3 = 079; //错误的八进制(>7)varnum4 = 09; //错误的八进制(>7)

The first two digits of the hexadecimal literal must be 0x, followed by any hexadecimal number (0~9 and a~f). Such as:

?
12 varnum1 = 0xA; //十六进制的10varnum1 = 0xa; //也是十六进制的10(字母不区分大小写)

Floating point value

It is important to note that you should never compare a specific floating point value. Such as:

?
12345 vara = 0.1;var b = 0.2;if(a + b == 0.3){  document.write("you are right")} //因为0.1 加上0.2 实际上等于0.30000000000000004

Range of values

The maximum and minimum values that ECMAScript can represent are preserved in Number.MAX_VALUE and Number.min_value. To determine whether a value is poor, you can use the Isfinite () function. Such as:

?
1 document.write(isFinite(Number.MAX_VALUE + Number.MAX_VALUE)); //false

NaN

0 divided by 0 will return Nan, a positive number divided by 0 will return Infinity, and the plural will return-infinity. Second, NaN is not equal to any numeric value, including itself. Another function, isNaN (), can help us determine if this parameter is "not a numeric value". Such as:

?
12345 document.write(isNaN("a")); //true;document.write(isNaN("324")); //false;document.write(isNaN(true)); //false;document.write(isNaN(false)); //false;document.write(isNaN(NaN)); //true;

numeric conversions

There are three functions that convert non-numeric values to numeric values: Number (), parseint (), parsefloat ().

The conversion rules for number () are as follows:

If it is a Boolean value, True and false are converted to 1 and 0.
If it is a numeric value, it does not change.
If it is a null value, it is converted to 0.
If it is undefined, return nan.
If it is a string, the following rules apply:

    • If there is only a number, it is converted to a decimal value.
    • If it is a floating-point format, it is converted to the corresponding floating-point value. Leading zeros are also ignored.
    • If it is in hexadecimal format, it is converted to a decimal number.
    • If the string is empty, it is converted to 0.
    • Other cases are converted to Nan.

See the following example:

?
123456789 document.write(Number(true)); //1document.write(Number(false)); //0document.write(Number("789")); //789document.write(Number(null)); //0document.write(Number(undefined)); //NaNdocument.write(Number("02.0942")); //2.0942document.write(Number(0xa)); //10document.write(Number("")); //0document.write(Number("fdsa")); //NaN

The conversion rules for parseint () are as follows:

    • If the first character is not a numeric character or symbol, parseint () returns Nan.
    • Converting an empty string with parseint () returns Nan.
    • If the first character is a numeric character, it continues to parse the second character until it encounters a non-numeric character.

Here's a concrete example:

?
12345678 document.write(parseInt("fds")); //NaNdocument.write(parseInt("")); //NaNdocument.write(parseInt("1111112abc")); //1111112document.write(parseInt("-1111112abc")); //-1111112document.write(parseInt("+1111112abc")); //-1111112document.write(parseInt("0xa")); //10document.write(parseInt("0022.00009")); //22document.write(parseInt("070")); //ECMAScript 3认为是56(八进制), ECMAScript 5认为是70(十进制)

It is also important to note that ECMAScript 5 does not have the ability to parse octal values, so in order to eliminate this problem, you can provide the second parameter for this function: the cardinality (how many bytes) to use when converting, as follows:

?
123 document.write(parseInt("070",10)); //70document.write(parseInt("070",8)); //56document.write(parseInt("070",16)); //112

In most cases, it is best to set the default to 10 binary.

The conversion rules for parsefloat () are as follows:

    • Similar to parseint (), the difference is that the first decimal point of the string is valid, and the second decimal point starting from the second decimal point is not valid.
    • He cannot parse hexadecimal values!!!
    • He can only parse decimal values!!!
    • He did not use a second base to specify the usage of the binary.

Here's a concrete example:

?
1234567 document.write(parseFloat("421")); //421document.write(parseFloat("0421.32.1")); //421.32document.write(parseFloat("0xaafd")); //0document.write(parseFloat("070")); //70document.write(parseFloat("070abc")); //70document.write(parseFloat("")); //NaNdocument.write(parseFloat("abc")); //NaN

String type

There are two ways to convert a value into a string. The first is the ToString () method, which uses almost every value. As follows:

?
12345 document.write((533).toString(10)); //"533"document.write((0xa).toString(10)); //"10"document.write((0xa).toString(2)); //"1010"document.write((true).toString(10)); //"true"document.write((false).toString(10)); //"false"

It is also important to note that null and undefined cannot be converted.

?
12 document.write((null).toString(10)); //document.write((undefined).toString(10)); //

If you do not know if the value you want to convert is null or undefined, you should use the Transform function string (), or null if it returns "null" if undefined returns "undefined". As follows:

?
12 document.write(String(null)); //"null"document.write(String(undefined)); //"undefined"
  • Complex data types

    • Object
      • An object that is instantiated by the type, which is a collection of data and functions (functions).
      • There are two procedures for instantiating an object, one through the new operator and one through the object literal notation.
      • The object type is the most basic type, and we can inherit more types on top of it, such as the array (), Date (), and interesting function () types that we know are inherited from Object, And these types are set by default by the programmer before the use of properties and methods, for the programmer to call.
      • And the above type has a general name as a reference type. Why is it a reference type?
          var obj=new Object();
        When learning C, know "int A; int &ra=a;, so RA is a reference, the type of RA is a reference type, and the system does not allocate memory space for reference types.
        However, JS refers to the type of reference, in the "JS Advanced Programming" This book, is also really the concept, obj is only a reference type, is a nickname for an object in the heap memory, itself does not occupy memory.
        1 but this book also says that access to reference types is accessed by reference, but when
        var obj2=obj;
        When the value of obj is returned to OBJ2, the address of the object in the heap memory is assigned to OBJ2, which seems to say that when declaring a reference type, the instantiated object address is stored in the stack memory, and then the heap memory is accessed through the address.
        2 There is a difficult point, but also when I learn the reference type of confusion, C, when the reference can be used to pass the parameters, and, the reference cannot be assigned, that is, the reference value cannot be changed. However, in JS, the function parameters are the value of the pass, and for the reference type in JS, its value, the default is the stack memory address.
      • JS basic data types and reference data types differ
      • A variable of the
      • JS base data type holds the actual value of the underlying type data, and a variable referencing the data type holds a reference to it, the pointer.

        js basic data type: null undefined number Boolean

        string more special

        reference data type: function Object array

        var a = one;

        var b = 1;

        B = n;

        Console.log (a);//11 A's value does not change as B is worth changing

         

         

        var a = [n/a];

        var b = A;

        A[0] = 2;

        Console.log (b[0])//2, the value of B is changed with the value of a, because they point to the same memory address

         

        var a = [n/a];

        var b = A;

        B = [one, one, 13];//b points to another memory address, and is associated with a disconnect

        A[0] = 2;

        Console.log (B[0]) The value of//11,b does not change with the value of a

         

        For example, var a = 1; 1 is the basic data type, so variable a holds the value 1.

            var b = [1,2,3],[1,2,3] is a reference data type, [one-off] is in memory, and the variable B holds the address of [a-c], rather than [all-in-one] value

            When you use the B variable, the JS interpreter will find the corresponding address in memory by using the memory address stored in B, and take the value out to return

        from:http://my.oschina.net/u/1992917/blog/359825

JavaScript Data type

Related Article

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.