Primitive data type

  • Primitive data typeInclude:String, Boolean, null, undefined, symbol, BigInt.
  • Features:
  • 1) Basic data typeThe value is immutablethe
  • 2) Basic data typesYou cannot add attributes and methods
  • 3) Basic data typeAssignment is simple assignment
  • 4) Basic data typeA comparison is a comparison of values
  • 5) The basic data type isStore in stack areathe

Reference data type

  • Reference data typeIn JavaScript, there are only reference data types in addition to basic data typesobject). Such as:Object, Array, Function, DataAnd so on. Features:
  • 1) Reference typeThe value can changethe
  • 2) Reference typeYou can add properties and methods
  • 3) Reference typeAssignment is an object reference
  • 4) Reference typeA comparison is a comparison of references
  • 5) The reference type isStored in both stack and heapthe

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 variables
  • Reference 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 valueWhen 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