Reading Notes on JavaScript advanced programming (2) Original Type _ javascript skills in ECMAScript

Source: Internet
Author: User
ECMAScript has five primitive types (primitivetype): Undefined, Null, Boolean, Number, and String. ECMAScript provides typeof to determine the value type 2.6 Original Type
ECMAScript has five primitive types: Undefined, Null, Boolean, Number, and String. ECMAScript provides typeof to determine the value type.
1. typeof OPERATOR:

The Code is as follows:


Var sTemp = "test string ";
Alert (typeof sTemp); // outpus "string"
Alert (typeof 95); // outpus "number"


There are only five types of return values for the typeof OPERATOR: If the variable is Undefined type, "undefined" is returned. If the variable is Boolean type, "boolean" is returned. If the variable is Number type, "number" is returned ", if the variable is String type, "string" is returned. If the variable is a reference type or Null type, "object" is returned ".
2. Undefined type
The Undefined type has only one value, that is, undefined. When the declared variable is not initialized and the function does not return a specific value, the default value of the variable and the return value of the function are undefined. Note that the undefined value is not the same as the undefined value, but typeof does not distinguish the two values. Refer to the following code:

The Code is as follows:


Var oTemp;
Alert (typeof oTemp); // outpus "undefined"
Alert (typeof otemp2); // outpus "undefined"
Alert (oTemp = undefined); // outpus "true"
Alert (oTemp2 = undefined); // causes error
Function testFunc (){
//
}
Alert (testFunc () = undefined); // outpus "true"


3. Null type
Null is also a type with only one value. It has only one dedicated value, null. The undefined value is not actually derived from the null value, so ECMAScript defines them as equal.
1 alert (null = undefined); // outpus "true"
Although these two values are equal, they have different meanings. Undefined is the value when a variable is declared but not initialized. null indicates an object that does not exist.
4. Boolean type
Boolean has two values: true and false.
5. Number Type
Number can be a 32-bit integer or a 64-bit floating point Number:

The Code is as follows:


Var iNum = 55; // 10 hexadecimal
Var iNum = 070; // octal
Var iNum = oxAB; // hexadecimal
Var fNum = 3.125e7; // scientific notation indicates a floating point number.


Several special values are also defined as the Number type. The first two are Number. MAX_VALUE and Number. MIN_VALUE. They define the outer boundary of the Number value set. The number of ECMAScript values must be between these two values, but the calculated value can not be between these two values.
When the Number generated by the calculation is greater than Number. MAX_VALUE, it will be assigned the value Number. POSITIVE_INFINITY, meaning no numerical value exists. Similarly, if the generated value is smaller than Number. MIN_VALUE, the value Number. NEGATIVE_INFINITY is also assigned, meaning that no numeric value exists. If the result returned by the calculation is infinite, the generated result cannot be used for other computations.
In fact, a special value represents Infinity, that is, Infinity. Number. The POSITIVE_INFINITY value is Infinity, and the Number. NEGATIVE_INFINITY value is-Infinity.
The isFinit () method can be called on any number to determine whether it is infinite. Example:

The Code is as follows:


Var iResult = iNum * some_really_large_number;
If (isFinit (iResult )){
Alert ("Number is finite ");
}
Else {
Alert ("Number is infinite ");
}


There is also a special value NaN, indicating a non-Number (Not a Number ). NaN is generally the value in case of type conversion failure. NaN cannot be used for arithmetic computation. Another peculiar feature of NaN is that it is not equal to itself. Therefore, isNaN () is recommended, for example:

The Code is as follows:


Alert (NaN = NaN); // outpus "false"
Alert (isNaN ("blue"); // outpus "true"
Alert (isNaN ("123"); // outpus "false"
Alert (isNaN (123); // outpus "false"


6. String type
String is the unique primitive type with no fixed size. ECMAScript literal:
Literal Meaning
\ N line feed
\ T Tab
\ B space
\ R press ENTER
\ F page feed
\ Backslash
\ 'Single quotes
\ "Double quotation marks
\ 0nnn octal characters represented by nnn
Characters represented by \ xnn hexadecimal code nn
\ Unnnn hexadecimal code: Unicode characters not allowed in the nnnn table
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.