It's written in front of you.
The data type of JavaScript can be divided into two types: original type and reference type
The original type, also known as the base type or simple type, is a simple data segment because it occupies a fixed space and is stored in stacks (stack) (accessed by value) in order to facilitate the speed at which variable queries are raised. Where the JavaScript basic data types include undefined, Null, Boolean, number, and string five kinds
A reference type cannot be stored on the stack because its value is sized, so it is stored in the heap (heap), and the value stored at the variable is a pointer to the memory where the object is stored (by Access)
[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 the base type cannot add properties and methods
Undefined
The undefined type has only one value, which is undefined. When a declared variable is uninitialized, the default value of the variable is undefined
var test;//undefined
console.log (test = = undefined);//true
A variable that has not yet been declared can only do one operation, using the TypeOf operator to detect its data type, but in strict mode results in an error
typeof (test);//undefined
Scene appears
[1] A variable with an unassigned value declared
[2] Get properties that do not exist for an object
[3] Execution result of function without return value
The parameter of [4] function is 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, or null. Logically, a null value represents an empty object pointer, which is best initialized to NULL if the defined variable is to be used to save the object. The undefined value is actually derived from a null value, so undefined = = NULL
[Note that the]null is a null object pointer and [] is an empty array, {} is a null object, and three are not the same
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, you cannot add custom properties
Packing type
The wrapper type is a special reference type. Whenever a base type value is read, the background creates an object of the corresponding base wrapper type, which may invoke methods to manipulate the data. The packing type includes three kinds of Boolean, number, and string
var S1 = ' some text ';
var s2 = s1.substring (2);
In the preceding procedure, three steps were actually taken to
var S1 = new String (' some text ');//(1) Create an instance of String type
var s2 = s1.substring (2);//(2) Invokes the specified method on the instance
[note] The primary difference between a reference type and a base wrapper type is the lifetime of the object. An instance of a reference type created using the new operator is kept in memory until the execution stream leaves the current scope. An object of the base wrapper type that is automatically created, only exists at the execution moment of a line of code and is immediately destroyed. This means that you cannot add properties and methods to base type values at run time
var S1 = ' some text ';
S1.color = ' red ';
How to create
There are two ways to explicitly create a wrapper type:
[1] object method [not recommended]
var s = new Object (' abc ');
var B = new Object (true);
[2] Constructor method [not recommended]
var s = new String (' abc ');
var B = new Boolean (true);
[note] Using new to invoke a constructor of the base wrapper type is not the same as a direct call to a transformation function with the same name
var value = ' a ';
var number = number (value);
Console.log (typeof number);//number
var obj = new number (value);
Boolean
The 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 easily cause misunderstanding
Scene appears
[1] A hermit type conversion of the system execution caused by a conditional statement
[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 (non 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 equal to 0
Method of wrapping 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 true
Console.log (typeof false.valueof (), False.valueof ());//boolean false
Console.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 '
Number
JavaScript has only one numeric type, which can represent 32-bit integers and 64-bit floating-point numbers.
More information about the number type moves to this
String
String type is the only original type in JavaScript that does not have a fixed size
Next, let's understand the wrapper object for the JavaScript basic data type.
phenomenon: Why can the manipulation of strings take the representation of objects?
For example:
var s = "This is a String";
var len
Analytical:
JavaScript three basic data types have corresponding object classes, respectively, Sring,number,boolean class;
JavaScript can be flexible in converting one type of value to another;
When we use a string in an object environment, that is, when we try to access the property or method of the string;
JavaScript creates a string wrapper object internally for this string value;
The string object temporarily replaces the original string value and completes our visit;
This string object created internally is instantaneous, and its function is to enable us to access properties and methods normally;
The string object is discarded by the system after it is used;
And the original values are not changed;
The same applies to numeric and Boolean types;
Using the object () function, any number, string, or Boolean value can be converted to its corresponding wrapper object;
For example:
var number_wrapper = Object (3);