Primitive data type
Primitive data type
Include:String, Boolean, null, undefined, symbol, BigInt
.- Features:
- 1) Basic data type
The value is immutable
the - 2) Basic data types
You cannot add attributes and methods
- 3) Basic data type
Assignment is simple assignment
- 4) Basic data type
A comparison is a comparison of values
- 5) The basic data type is
Store in stack area
the
Reference data type
Reference data type
In JavaScript, there are only reference data types in addition to basic data typesobject
). Such as:Object, Array, Function, Data
And so on. Features:- 1) Reference type
The value can change
the - 2) Reference type
You can add properties and methods
- 3) Reference type
Assignment is an object reference
- 4) Reference type
A comparison is a comparison of references
- 5) The reference type is
Stored in both stack and heap
the
The difference between:
1) Different memory allocation when declaring variables
The original type
: In the stack, because the space occupied is fixed, they can be stored in a smaller memory – stack, so that it is easy to quickly query the value of variablesReference types
: a variable stored in the heap and used only to look up reference addresses in the heap.2) Different access mechanisms
- Javascript does not allow you to access objects stored in the heap directly, so when you access an object, you first get the address of the object in the heap, and then use that address to get the value of the object. This is called access by reference.
- Primitive values are directly accessible.
3) Differences in copying variables
The original value
: When a variable holding the original value is copied to another variable, the copy of the original value is assigned to the new variable. After that, the two variables are completely independent, they just have the same value.Reference value
When you copy a variable that holds the memory address of an object to another variable, the memory address is assigned to the new variable. That is, both variables refer to the same object in heap memory, and changes made by either of them are reflected in the other variable.
Null and undefined
- Simple to understand:
Undefined indicates that the value does not exist, and null indicates that the value exists but is empty and meaningless
;
null= =undefined // true
null= = =undefined // fasle
// typeof null is Object, typeof undefined is undefined
Copy the code
Null: As the end of the object prototype chain
- The following cases are undefined
- 1) When defining a variable without assigning a value.
- 2) When calling a property that has not been added to an object.
- 3) When calling a function that does not return a value.
Chestnut 🌰 :let a; // undefined
let b = {};
b.name; // undefined
function c() {};
let d = c(); // undefined
Copy the code