Data types in JavaScript

Source: Internet
Author: User

JavaScript contains five basic data types (raw data types), namely: Undefined, NULL, number, Boolean, string; and a reference type Object, the reference type contains a special class: the function type. Number, Boolean, String three basic types for a wrapper type number,boolean,string, these three wrapper types are object types.

For these types, the following are explained separately:

1. typeof operator

You can get the type of the variable by this operator, the operation return value is a string, the following 7 kinds: "Number", "string", "boolean", "null", "undefined", "function", "Object".

2. Basic types

Undefined type: There is only one value undefined, when you declare a variable with VAR but do not initialize it, the value of this variable is undefined

var sum;    // The default value of undefinedalert (sum)  //  "undefined"alert (age)    is not initialized after declaration Variable not declared, error  = = undefined)       //  truealert (typeof SUM)            //  "undefined"alert (typeof Age)            //  "undefined" the     typeof of undeclared variables here also returns "undefined", which is logically justified, indicating that the variable cannot be manipulated

Null type: is also a data type with only one value, and a value of NULL, from a logical point of view, NULL represents an empty object pointer.

var NULL ; alert (typeof title);    // "Object" // because performing a typeof operation on a variable that is assigned null returns an object, //  //  This only needs to detect if it is null, to know if it has saved a reference to an object ifnull) {      // do something}

Number, Boolean, String type, and its wrapper type:

varnum = 5;varNUM =NewNumber (5);//Create a Number object hereAlerttypeofNUM);//"Number"AlerttypeofNUM);//"Object"varNUMF = Number (5); alert (typeofnumber);//"function" here number is a wrapper functionAlerttypeofNUMF);//"Number"//Boolean and string types are similar to numbervarBOOL =false;varBOOL =NewBoolean (false); alert (typeof(bool); alert (typeofBOOL);varstr = ' Hello ';varSTR =NewString (' Hello '); alert (typeofstr); alert (typeofSTR);

3. NaN: is a special value that indicates that a value should be returned without returning a numeric value, for example: except 0, Number ("a"), etc.

//1. Any operation that involves Nan will return NanAlert (1+nan)//NaNAlert (NAN/10)//NaN//2.NaN is not equal to any valueAlert (nan = = Nan)//falseAlert (2 = = NaN)//falseAlert ("abc" = = NaN)//false//3. IsNaN () function: values that cannot be converted to numbers return trueAlert (IsNaN (5))//falseAlert (IsNaN (true))//falseAlert (IsNaN ("a"))//true

4. Reference type: A set of data and features. An object is an instance of a reference type.

[[Class]] is an intrinsic property that can be used to classify an object, which has the following values:

"Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "number", "Object", RegExp "," String "

JavaScript can only get this intrinsic property through the ToString () method (Object.prototype.toString ())

Object.prototype.toString.call (undefined)//"[Object Undefined]"Object.prototype.toString.call (NULL)//"[Object Null]"Object.prototype.toString.call (function(){})//"[Object Function]"Object.prototype.toString.call (Math)//"[Object Math]"Object.prototype.toString.call (Array)//"[Object Function]"Object.prototype.toString.call ([])//"[Object Array]"Object.prototype.toString.call ({})//"[Object Object]"//the underlying type is converted to the wrapper type according to the corresponding rule processingObject.prototype.toString.call ("abc")//"[Object String]"Object.prototype.toString.call (200)//"[Object number]"Object.prototype.toString.call (true)//"[Object Boolean]"

You can get the object type by using the following function:

functiongetclass (x) {varstr =Object.prototype.toString.call (x); return/^\[object (. *) \]$/.exec (str) [1]; }getclass (NULL);//"Null"GetClass ({})//"Object"GetClass ([])//"Array"GetClass (JSON)//"JSON"GetClass (function(){})//"Function"functionFoo () {}getclass (NewFoo ())//"Object"

Data types in JavaScript

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.