Difficulty ratings range from five stars to one star.

A:

Both Typeof and Instanceof can be used to determine data types

  • typeofReturns the type string of a variable,instanceofIt returns a Boolean value
  •  typeofIt can be judged that in additionnullOther than the underlying data type, but when determining the reference type, exceptfunctionType, others can not be accurately determined.
  • instanceofYou can accurately determine the various reference types, but not the raw data types.

typeof

The typeof operator returns a string representing the typeof the unevaluated operand.

Typeof can accurately determine primitive types, but not null and reference types.

Reference type data. If you use typeof, all output objects except function will be recognized

/ / values
typeof 37= = ='number'
typeof 3.14= = ='number'
typeof (42) = = ='number'
typeof Math.LN2 === 'number'
typeof Infinity= = ='number'
typeof NaN= = ='number' // Although it is short for "not-a-number"
typeof Number(1) = = ='number' // Number attempts to parse the argument to a numeric value

typeof 42n= = ='bigint'

/ / string
typeof ' '= = ='string'
typeof 'bla'= = ='string'
typeof 'template literal'= = ='string'
typeof '1'= = ='string' // Note that a numeric string is still a string
typeof (typeof 1) = = ='string' // Typeof always returns a string
typeof String(1) = = ='string' // String converts any value to a String, which is safer than toString

/ / a Boolean value
typeof true= = ='boolean'
typeof false= = ='boolean'
typeof Boolean(1) = = ='boolean' // Boolean() converts based on whether the argument is true or imaginary
typeof!!!!! (1) = = ='boolean' // Two calls! The (logical non) operator is equivalent to Boolean()

// Symbols
typeof Symbol() = = ='symbol'
typeof Symbol('foo') = = ='symbol'
typeof Symbol.iterator === 'symbol'

// Undefined
typeof undefined= = ='undefined'
let a
typeof a === 'undefined' // declaredButUndefinedVariable
typeof undeclaredVariable === 'undefined' // undeclaredVariable

// Null
typeof null= = ='object' / / history bug

/ / object
typeof { a: 1} = = ='object'

/ / using Array. IsArray or Object. The prototype. ToString. Call
// Distinguish between arrays and ordinary objects
typeof [1.2.4= = ='object'

typeof new Date() = = ='object'
typeof /regex/ === 'object' // Regular expressions are also objects

// The following example is confusing, dangerous and useless. Avoid them.
typeof new Boolean(true) = = ='object'
typeof new Number(1) = = ='object'
typeof new String('abc') = = ='object'

/ / function
typeof function () = = = {}'function'
typeof class C = = = {}'function'
typeof Math.sin === 'function'
Copy the code

If we want to check whether a variable exists, we can use typeof :(cannot use if(a), if a is not declared, an error is reported)

if (a) {
  // Uncaught ReferenceError: a is not defined
}

if (typeofa ! = ='undefined') {
  // This will not report an error
}
Copy the code

instanceof

The instanceof operator is used to check whether the constructor’s prototype property appears on the prototype chain of an instance object.

class Person {}

class Student extends Person {}const s1 = new Student()

s1 instanceof Student // true
s1 instanceof Person  // true
s1 instanceof Object  // true
Copy the code

It can also be used to determine the type, but it must be used to determine the object instance.

const simpleStr = 'This is a simple string'
const myString = new String(a)const newStr = new String('String created with constructor')

simpleStr instanceof String // false, not an instance of the object, so return false, cannot determine the original type
myString instanceof String // true
newStr instanceof String // true
Copy the code

Using Instanceof allows you to accurately determine the type of reference.

[] instanceof Array   // true

const fn = function() {}
fn instanceof Function // true

const date = new Date()
date instanceof Date   // true

const re = new /abc/
re instanceof RegExp   // true

Copy the code

At the end

This is the 36th day of alynn’s continuous blog post, exporting insight technology, goodbye!

If my article is helpful to you, your 👍 is my biggest support ^_^