"JavaScript" Data type

Source: Internet
Author: User

Learning any programming language, data types are an essential part of the content, very basic, but also very important. Which data type defines the variable, which is the most basic of the programming.  There are 5 simple data types in ECMAScript: Undefined, Null, Boolean, number, and string. There is also a complex data type of--object. Any mechanism for creating custom types is not supported in ECMAScript, and all values become one of the above 6 data types.  in ECMAScript, the data type of a variable is detected using the TypeOf operator. For example: var box = ' Li Tinghui ';  alert (typeof box);  The output value is a string type.  Here is a mind map of a summarized data type:


Six data types, I divide it into two categories, one is that regardless of the variable, the output value under the data type is deterministic, including the Undefined, Null, Boolean, and number types, and the other is the variable, the output value under the data type is indeterminate, Includes the string type and the object type. The following 6 data types are described in detail:I. Undefined typeThe 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.In particular,Uninitialized variables are not the same as variables that do not exist at all (undeclared variables)./*box belongs to uninitialized variableAge belongs to an undeclared variable*/var box;alert (age); Output Age Valuealert (box); Output box valueAn error is defined when outputting the age value, and an undefined value is output when the box value is output. But their types are undefined.So, when defining a variable, don't just declare it, not assign a value.two. Null typeThe type has only one value, that is, a special null value. It represents an empty object reference (pointer), uses the typeof operator to detect null, and returns an object.In particular,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, so that when the null value is checked, the variable is already assigned an object reference. Such as:var box=null;if (box! = null) {Alert (' box object already exists ');
Three. A Boolean type  has two values: True and false (case-sensitive).  in particular,  other types are converted to a Boolean type rule:


four. Number TypeThe type contains two types of values: integer and floating-point.In particular,1.NaN, that is, non-numeric (not a number) is a special value.2.3 Functions that convert non-numeric values to numeric values: Number (), which can be used with any data type, parseint () and parsefloat (), which are specifically used to turn a string into a numeric value.3.Infinity (positive Infinity) and-infinity (negative infinity)4. Check if a value exceeds the specified range, isfinite () function. Returns true if it is not exceeded, and returns false.Five. String TypeThe string type, denoted by double quotation marks (") or single quotation marks ('), must appear in pairs.In particular,1. Once a string is created, their value cannot be changed. To change, you must first destroy the original string and then populate it with a string that contains the new value.Such as:var box = ' Mr. ';box=box+ ' Lee ';The 2.toString () method can convert a value to a string. Such as:var box=11;Alert (typeof box.tostring ());
Six.  objects in object type ECMAScript are actually a collection 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.  Example:var box=new Object ();  in particular,the Object () can be arbitrarily passed, passing values, strings, Boolean values, and so on. Furthermore, the corresponding calculations can be made.   Example:var box=new Object (2);                      //object Type, value is 2;var age=box+2;                        //Can and ordinary variable operation;alert (age); The output is transformed to the number type.  data type has been contacted many times, each time in the learning of the same knowledge, there will always be progress, there is a new harvest, warm so know new, so also need us to take seriously.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"JavaScript" Data type

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.