preface

When we learn programming, the first contact is the data type, every language is its own data type, JavaScript is no exception, JavaScript data type mainly includes two large types of basic type and reference type, next we will analyze the difference between these two types.

The data type

Basic types of

  • Number Number type
  • String String type
  • Boolean Indicates the Boolean type
  • Null Null type
  • Undefined undefined type

Reference types

  • Object Object type

The difference between the two

The storage of the base type is stored in the stack, while the storage of the reference type is stored in the heap. The base variable stores the specific value, and the reference type stores the address in the heap, as shown in the figure below

Why are base types and reference types stored differently

  • The heap is larger than the stack, and the stack is faster than the stack.
  • The underlying data types are stable and relatively memory intensive.
  • The reference data type size is dynamic and infinite.
  • Heap memory is unordered storage and can be retrieved directly by reference.

Detection data type

We have the typeof, instanceof, methods to detect type of the Object. The prototype. ToString. Call (), etc

  • typeof

Typeof can detect most data types, function can also detect, null and Array can detect object, Array is a reference type, detect object is not a problem. But null being of type object is a bug, and it has existed in JavaScript for a long time

   console.log(typeof 2)    // number
   console.log(typeof '2')  // string
   console.log(typeof true) // boolean
   console.log(typeof undefined) // undefined
   console.log(typeof null) // object
   console.log(typeof function() {})   // function
   console.log(typeof [])   // object
   console.log(typeof {})   // object
Copy the code
  • instanceof

Instanceof is used to check whether the prototype property of the constructor appears on the prototype chain of an instance object. The constructor === constructor can be used to check whether the prototype property of the instance object appears on the prototype chain

class People {}
class Student extends People {}
let s = new Student
console.log(s instanceof Student)   // true
console.log(s instanceof People)   // true
console.log(s instanceof Object)   // true
Copy the code
  • Object.prototype.toString.call()

The principle is to use the native toString() method on Object.prototype to determine the data type

// Boolean type, tag is "Boolean"
Object.prototype.toString.call(true);            // => "[object Boolean]"
// Number type, tag is "Number"
Object.prototype.toString.call(1);               // => "[object Boolean]"
// The tag is "String".
Object.prototype.toString.call("");              // => "[object String]"
// Array type, tag "String"
Object.prototype.toString.call([]);              // => "[object Array]"
// Arguments type, tag = "Arguments"
Object.prototype.toString.call((function() {
  return arguments; }) ());// => "[object Arguments]"
// Function type, tag is "Function"
Object.prototype.toString.call(function(){});    // => "[object Function]"
// Error type (including subtypes), tag "Error"
Object.prototype.toString.call(new Error());     // => "[object Error]"
// RegExp type, tag "RegExp"
Object.prototype.toString.call(/\d+/);           // => "[object RegExp]"
// Date type, tag is "Date"
Object.prototype.toString.call(new Date());      // => "[object Date]"
// Other type, tag is "Object"
Object.prototype.toString.call(new class {});    // => "[object Object]"
Copy the code

subsequent

  • There are two types of assignment for reference variables: shallow copy and deep copy
  • In ES5, there are six data types: Object, Number, String, Boolean, Null, undefined
  • In ES6 ES7 and ES5 on the basis of the new Symbol, BigInt