Javascript series: original ecmascript type

Source: Internet
Author: User

Ecmascript has five primitive types: undefined, null, Boolean, number, and string.

1. typeof Operator

Ecmascript provides the typeof operator to determine whether a value is within a certain type. You can use this operator to determine whether a value represents a primitive type. If it is a primitive type, you can also determine which primitive type it represents.

The typeof operator has a parameter, that is, the variable or value to be checked.

When the typeof operator is called for a variable or value, one of the following values is returned:

Else"Undefined"-- Undefined type variable

Else"Boolean"-- Boolean Type Variable

Else"Number"-- Number type variable

Else"String"-- String type variable

Else"Object"-- Reference type or null type variable

VaRSTR = 'test'; Alert (TypeofStr );//Output "string"Alert (Typeof100 );//Output "Number"

2. undefined type

The undefined type has only one value, that is, undefined. When the declared variable is not initialized, the default value of this variable is undefined.

 
VaROtemp; alert (otemp= Undefined );//Output "true"Alert (TypeofOtemp );//Output "undefined"

Typeof returns "undefined" for undeclared variables ".

 
//VaR otemp;Alert (TypeofOtemp );//Output "undefined"

If the function does not return a value, the returned value is undefined.

 
FunctionFntest (){//Leave the function Blank} Alert (fntest ()= Undefined );//Output "true"

3. null type

Another type with only one value is null. It has only one special value, I .e. its literal value. The undefined value is actually derived from the null value, so ecmascript defines them as equal.

Alert (Null= Undefined );//Output "true"

Although the two values are equal, they have different meanings. Undefined indicates the value of a variable that is declared but not granted to it during initialization. null indicates an object that does not exist.

4. boolean type

The boolean type is one of the most common types in ecmascript. It has two values: true and false.

5. Number Type

The most special type defined in the ECMA-262 is the number type. This type can be a 32-bit integer or a 64-bit floating point number. Any number directly entered is treated as the nominal value of the number type. Number has a special value Nan, indicating a non-number (not a number ). Nan occurs when type (string, Boolean) Conversion fails. Nan cannot be used for arithmetic computation. Nan is not equal to itself.

Use the isnan () function to determine whether it is a non-number.

Alert (Nan = Nan );//Output "false"Alert (isnan ("Blue "));//Output "true"Alert (isnan ("123 "));//Output "false"

6. string type

The unique feature of the string type is that it is the only primitive type with no fixed size. You can use a string to store 0 or more Unicode characters, expressed by a 16-bit integer.

Each character in a string has a specific position. The first character starts from the position 0, the second character is at the position 1, and so on.

The string literal is declared by double quotation marks (") or single quotation marks. Because ecmascript does not have a character type, either of these two representations can be used.

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.