JavaScript notes: Data types

Source: Internet
Author: User

There are 5 basic data types in javascript:undefined,null,boolean,number and String, and a complex data type -- Object . JavaScript does not support any mechanism for creating custom types, and all values become one of the above six types.

one, typeof operator

The typeof operator is the data type used to detect variables. Using the typeof operator for a value or variable returns the following string.

For example:

var box = ' Xiao Xu '; alert (typeof box);   // Eject String

one, undefined type

the Undefined type has only one value, that is, a special Undefined. When you declare a variable using VAR, but you do not initialize it, the value of the variable is undefined.

PS: Uninitialized variables and variables that do not exist at all ( non-declared variable ) is not the same. The following code:

// Age isn't defined var  //undefined

But using the typeof operator to detect their return string is undefined. So when we define variables, we try not to just declare them as unassigned.

var Box;alert (typeof//undefinedalert (typeof//undefined 

One, Null type

null null ( Pointer ) typeof operator detection null will return object

PS: If the defined variable is intended to be used to hold the object in the future, it is better to initialize the variable to null. This way, when the null value is checked, you know whether the variable has already been assigned an object reference.

var NULL ; alert (typeof Box);

One, Boolean type

Booleantype has two values(literal quantity):trueand thefalse. AlthoughBooleantype literal is onlytrueand thefalsetwo kinds, butECMAScriptall types of values in these twoBooleanthe value equivalent value. To convert a value to its correspondingBooleanvalue, you can use the transform functionBoolean ().

var hello = ' Hello world! ' ; var hello2 = Boolean (hello); alert (Hello2); // Popup True

above is a display conversion, which is a mandatory conversion. In practical applications, there is also an implicit conversion. For example, There is an implicit conversion in the conditional judgment within the IF condition statement.

            var hello = ' Hello world! ' ;             if (hello) {                alert (' If the condition is true, execute me! ')            }else{                alert (' If the condition is false, execute me! ');                }

The following are other types that convert to Boolean Type Rule

first, Number type

The number type contains two values: integer and float. To support various numeric types,ECMA-262 defines different numeric literal formats

var box = +;            // decimal Integer var box = 070;            // eight-in-a-year, var box = 0xA;            // 16 binary, ten var box = 3.8;            // floating-point types

NaN, that is, a non-numeric (not a number) is a special value that represents a case where an operand that would have returned a numeric value does not return a value ( so that no error is thrown ). ECMAScript provides a IsNaN () function to determine whether the value is NaN. the IsNaN () function attempts to convert the value to a numeric value after it receives one.

Alert (IsNaN (NaN));                // truealert (IsNaN);                // false,25 is a numeric alert (IsNaN ('));              // false, ' 25 ' is a string value that can be converted to a numeric alert (IsNaN (' Lee '));             // true, ' Lee ' cannot convert to numeric alert (IsNaN (true));              // false    True to turn into 1
One, String type

The string type is used to represent a sequence of characters consisting of 0 or more Unicode characters, that is, a string. The string can be represented by double quotation marks (") or single quotation marks (') .

var box = ' Lee '; var box = "Lee";

The toString () method converts a value to a string.

var box = one; alert (typeof// Popup string

I. type of Object

the object in ECMAScript is actually a set of data and functions. An object can be created by executing The new operator followed by the name of the object type to be created.

var New Object ();

JavaScript notes: Data types

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.