Basic data types and package types for JavaScript type systems

Source: Internet
Author: User

The data types of JavaScript can be categorized into two types: primitive type and reference type

The primitive type, also known as the base type or simple type, because it occupies space fixed, is a simple data segment, in order to facilitate the variable query speed, stored in the stack (by value access). where JavaScript basic data types include undefined, Null, Boolean, number, and string five

The reference type cannot be stored in the stack because its value is changed, so it is stored in the heap, and the value stored at the variable is a pointer to the memory of the storage object (accessed by site).

[note] For values of reference types, you can add properties and methods to them, and you can change and delete their properties and methods, but you cannot add properties and methods to the base type

Undefined

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

var test;//undefinedconsole.log (test = = undefined);//truevar test = undefined;//undefined

You can only perform one operation on a variable that has not been declared, use the typeof operator to detect its data type, but strict mode causes an error

typeof (test);//undefined

Scene appears

[1] Non-assignable variable declared

[2] Getting properties that do not exist for an object

[3] Execution result of a function without return value

[4] The parameters of the function are not passed in

[5]void (expression)

Type conversions

Boolean (undefined): false

Number (undefined): NaN

String (undefined): ' Undefined '

Null

A null type has only one value, which is null. At a logical angle, a null value represents an empty object pointer, and if the defined variable will be used to hold the object, it is best to initialize the variable to null. The undefined value is actually derived from a null value, so undefined = = NULL

[Note that]null is an empty object pointer, while [] is an empty array, {} is an empty object, and the three are not the same

Console.log (null = = undefined);//true    

Scene appears

When an object does not exist

Type conversions

Boolean (NULL): False

Number (NULL): 0

String (NULL): ' NULL '

[note] because undefined and null are not constructor types, custom properties cannot be added

Package Type

The wrapper type is a special reference type. Whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type, which may invoke some methods to manipulate the data. Package types include a total of three Boolean, number, and string

var S1 = ' some text '; var s2 = s1.substring (2);//In the above process, there are actually three steps var S1 = new String (' some text '); (1) Create an instance of string type  var s2 = s1.substring (2);//(2) Call the specified method on the instance S1 = NULL;//(3) Destroy this instance

[note] The primary difference between a reference type and a basic wrapper type is the lifetime of the object. An instance of a reference type created with the new operator is kept in memory until the execution flow leaves the current scope. Objects that are automatically created by the basic wrapper type exist only in the execution of a single line of code and are immediately destroyed. This means that you cannot add properties and methods to the base type value at run time

var S1 = ' some text '; s1.color = ' red '; alert (s1.color);//undefined
How to create

There are two ways to create an explicit wrapper type:

[1] object mode [not recommended]

var s = new Object (' abc '); var b = new Object (true); var n = new Object (123);

[2] constructor mode [not recommended]

var s = new String (' abc '), var b = new Boolean (true), var n = new number (123);

[note] The constructor that calls the base wrapper type with new is not the same as a transformation function that calls the same name directly

var = ' + ', var number = number (value), Console.log (typeof number),//numbervar obj = new number (value), Console.log (Ty peof obj);//object

Boolean

A Boolean type has only two values: True and False. A Boolean wrapper type is a reference type that corresponds to a Boolean value, and using a Boolean object in a Boolean expression can be misleading

Scene appears

[1] A conditional statement causes the system to perform a hermit type conversion

[2] literal or variable definition

Type conversions

Number (TRUE): 1 | | Number (false): 0

String (True): ' True ' | | String (False): ' False '

Boolean ()

Boolean (undefined): false

Boolean (NULL): False

Boolean (non-empty object includes empty array [] and empty Object {}): True

Boolean (not 0): true | | Boolean (0 and Nan): false

Boolean (non-empty including space string): true | | Boolean ("): false

[Note that]true is not necessarily equal to 1,false nor necessarily equal to 0

Method of wrapper Type inheritance

ValueOf (): Returns the base type value TRUE or False

ToString () and toLocaleString (): Returns the string ' true ' or ' false '

Console.log (typeof true.valueof (), true.valueof ()),//boolean Trueconsole.log (typeof false.valueof (), false.valueof ( );//boolean Falseconsole.log (typeof true.tostring (), true.tostring ());//string ' true ' Console.log (typeof False.tostring (), false.tostring ()),//string ' false ' Console.log (typeof true.tolocalestring (), true.tolocalestring ( ));//string ' true ' Console.log (typeof false.tolocalestring (), false.tolocalestring ());//string ' false '

Number

JavaScript has only one numeric type, which can represent 32-bit integers, and can represent 64-bit floating-point numbers

For more information about the number type, go to this

String

The string type is the only original type in JavaScript that does not have a fixed size

For more information about string types

Basic data types and package types for JavaScript type systems

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.