Data Type and typeof Operator

Source: Internet
Author: User

Although I have been learning JavaScript For a while, I am a little unfamiliar with the basic syntax of JavaScript. Looking at jquery Source Code recently, I decided to summarize some basic syntax knowledge. Today, I will summarize the data types and typeof, which are the only thing I have to know when writing JavaScript code.

  Data Type:

JS has 5 basic data types: undefined, null, Boolean, number, and string. There is a complex data type: object. js does not support all custom data types, therefore, the values of all variables are of the above type.

  Typeof:Its appearance is used to detect the Data Type of a variable. There are 6 possible values.

Undefined --- if this value is not defined (the variable is not declared | only the declaration is not initialized | the declaration is initialized to undefined)

Boolean, number, String, object, function.

  Undefined data type:This data type has only one undefined value.

// First example var M1; // declared only, not initialized var m2 = undefined; // declare initialization undefinedalert (M1); // undefinedalert (m2 ); // undefinedalert (typeof M1); // undefinedalert (typeof m2); // undefined // The first example shows that after the variable is declared, automatically assigned undefined and the data type is undefined // The second example alert (n); // n is not declared, an error alert (typeof N) is generated ); // undefined // The second example shows that the data type of the undefined variable value is undefined.

  NULL data type:This data type has only one null value. null indicates a null object pointer. When defining variables, if you want to store objects, VAR n = NULL should be like this; null and undefined can be distinguished.

var n=null;alert(typeof n);//object

  Boolean data type:This data type has only two values: true and false, case sensitive. Although there are only these two values, all types of values can be converted to these two values (you can use Boolean () transformation function), which is very useful in if control statements. The conversion relationship is as follows:

Data Type Convert to true Convert to false
Boolean True False
Undefined None Undefined
Object Any object Null
String Any non-null characters NULL Character
Number Any non-zero number Null and Nan

 

NUmber data type:

  The special value is Nan (non-numerical value, which is a special value). This value indicates the case where the operand that originally returned the value is not returned. And it has two strange features: 1. Any operation on Nan will return Nan, 2. Nan is not equal to any value, including itself. Because of these features, ecmascript defines a function isnan (). Through this function, we can determine the values of any data type to determine whether these values are numerical values. Before Judgment, the isnan () function will first convert the values that can be converted (values of the boolean data type are true and false can be converted to 1, 0, respectively; the value string of a string can be converted to the corresponding value). During the determination process, if it is a value, false is returned. If it is not a value, true is returned. As follows:

Alert (isnan ("myname"); // truealert (isnan ("123") // false, the string is first converted to the value alert (isnan (true )) // false, which is converted to 1 alert (isnan (NAN) // true

 

String and object data types:Is there anything special in the data type detection? It is not within the scope currently discussed.

  

 

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.