Basic data types

console.log(typeof 123); // number
console.log(typeof 'hello'); // string
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof null); // object
console.log(typeof Symbol()); // symbol ES6 new data type
Copy the code

Reference data type

function add() {}const arr = [];
const obj = {}
console.log(typeof add); // function
console.log(typeof arr); // object
console.log(typeof obj); // object
Copy the code

The basic datatypes that can be evaluated by Typeof are null and reference datatypes only function.

So here’s the question.

  1. How to determine if a data is null?
  2. How to determine if a data is an Array?
  3. How can you tell if data is a pure object? (Not null and not array)

Let’s answer them all.

1. How to determine if a data is null?

const a = undefined;
const b = null;
console.log(a == null); // true therefore ==null may be undefined or null, not feasible
console.log(b == null); // true 
Copy the code

It’s worth noting

console.log(null= =undefined); // true
console.log(null= = =undefined); // false
Copy the code
console.log(a === null); // false
console.log(b === null); // true therefore === =
Copy the code

Conclusion: Use ===

2. How to identify a data Array?

Here are four ways to do it.

const arr = [];

console.log(Array.isArray(arr)); // true
console.log(arr instanceof Array); // true
console.log(arr.constructor === Array); // true
console.log(Object.prototype.toString.call(arr) === '[object Array]'); // true
Copy the code

3. How to determine if a data is a pure object? (Not null and not array)

const obj = {};

console.log(Object.prototype.toString.call(obj) === '[object Object]'); // true 
Copy the code

Note that the first letter of object object is lowercase and the second letter is uppercase

To the end.