Three kinds of judgment

1. The typeof operator

2. The instanceof operator

3. The Object. The prototype. The toString method

JS data type

Basic data types

number boolean string null undefined Symbol

Reference types

1.Object Array Function

2.Boolean String Number NaN Infinity

2.Date RegExp Error

typeof

Used to determine basic data types

typeof 124 // 'number'
typeof true // 'boolean'
typeof '12' // 'string'
typeof null // 'object'
typeof undefined // 'undefined' 
const a = Symbol('a') 
typeof a // 'symbol'
Copy the code

Note: Typeof determines null as ‘object’

typeof null // 'object'
Copy the code

Used to determine the type of reference

typeof [] // 'object'
typeof {} // 'object'

typeof f() {} // SyntaxError: Unexpected token '{'
 
const f = function() {}
typeof f // 'function'
Copy the code

Note: Typeof is used in func as a function, not all ‘object’

instanceof

A instanceof B: Determine whether B is on the prototype chain of A

[] instanceof Array // true

{a: 123} instanceof Object  // Unexpected token 'instanceof'
function(){} instanceof Function // SyntaxError: Function statements require a function name
function a() {} instanceof Function // SyntaxError: Unexpected token 'instanceof'

Symbol('a') instanceof Symbol // true
Copy the code