"JavaScript advanced Programming" Reading notes (ii) original type _javascript techniques in ECMAScript

Source: Internet
Author: User
Tags numeric numeric value
2.6 Original Type
ECMAScript has 5 original types (primitive type), that is, undefined, Null, Boolean, number, and string. ECMAScript provides a typeof to determine the type of value.
1, typeof operator:
Copy Code code as follows:

var stemp= "test string";
Alert (typeof stemp);//outpus "string"
Alert (typeof);//outpus "Number"

The typeof operator returns a value of only 5, respectively: If the variable is a undefined type returns "undefined" if the variable is a Boolean return "Boolean", if the variable is a number type returns "Number", If the variable is a string, returns "string" If the variable is a reference type or a null type returns "Object".
2, undefined type
The undefined type has only one value, that is, undefined. The default value of the variable and the return value of the function are undefined when the declared variable is uninitialized and the function does not have an explicit return value. Note that the value undefined differs from the undefined value, but TypeOf does not distinguish between the two values. Refer to the following code:
Copy Code code 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, and it has only one private value null. Value undefined are not actually derived from value NULL, so ECMAScript defines them as equal.
1 alert (Null = = undefined); Outpus "true"
Although the two values are equal, they have different meanings. Undefined is a value that declares a variable but does not initialize it, and null is used to represent an object that does not exist.
4. Boolean type
Boolean has two values true and False
5, Number type
Number can represent a 32-bit integer, and can also represent a 64-bit floating-point number, which is represented by different systems:
Copy Code code as follows:

var inum=55;//10 System
var inum=070;//8 System
var inum=oxab;//16 system
The scientific counting method of Var fnum=3.125e7;//to represent floating point numbers

Several special values are also defined as the number type, the first two are Number.MAX_VALUE and number.min_value, and they define the outer bounds of the set of values. All ECMAScript numbers must be between these two values, but the computed result of the calculation can not fall between these two numbers.
When the number of calculations generated is greater than Number.MAX_VALUE, it is assigned a value of number.positive_infinity, meaning that there is no longer a numeric value. Similarly, the resulting value of a number less than Number.min_value is also assigned a number.negative_infinity, meaning there is no longer a numeric value. If the result returned by the calculation is infinite, the resulting result cannot be used for other calculations.
In fact, there are special values that represent infinity, or infinity. The value of the number.positive_infinity is Infinity,number.negative_infinity-infinity.
Any number of calls to the Isfinit () method can be used to determine whether infinity is possible. Cases:
Copy Code code 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 of Nan, which represents the non-number (not a). Nan is typically the value of a type conversion failure, Nan cannot be used for arithmetic calculations, and the other peculiarity of Nan is that it is not equal to itself, so it is recommended to use isNaN (), such as:
Copy Code code 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
A string is the only original type that does not have a fixed size. Character literal of ECMAScript:
literal meaning
\ n Line Change
\ t tab
\b Space
\ r Carriage Return
\f Page Breaks
\ reverse Slash
\ ' Single quotes
\ "Double Quotes
\0NNN octal the characters represented by the code NNN
\xnn 16 The character that the code NN represents
\UNNNN 16 Code NNNN table non-Unicode characters
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.