I believe that when it comes to how to judge the data typeof js, everyone will think of typeof, instanceof, so why there is typeof existence also instanceof?

Typeof?

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

Eg:

typeof 1; // 'number'
typeof NaN; // 'number'
typeof 'zenquan'; // 'string'
typeof true; // 'boolean'
typeof null; // 'object'
typeof undefined; // 'undefined'
typeof Symbol(a);// 'symbol'
typeof console.log // "function"
Copy the code

Typeof null === ‘object’

And then you’ll notice, typeof NULL; / / ‘object’. Null is a primitive datatype. In the original version of JS, the 32-bit system was used to store the type information of the variable in the low level for performance purposes. The beginning of 000 represents an object, while null represents all zeros, so it was wrongly judged as object. Although the internal type determination code has changed, the Bug has persisted.

Typeof problems 2 — typeof reference type | | Math = = = ‘object’

You don’t know if it’s an array or an object

typeof [] // "object"
typeof {} // "object"
Copy the code

Typeof problems in basic packing type 3 — typeof | | Array | | Date = = = ‘funtion

You can’t tell if it’s Number or Boolean or String

typeof Number // "function"
typeof Boolean // "function"
typeof String // "function"
Copy the code

Instanceof?

Typeof has these problems, hence Instanceof.

The MDN: instanceof operator is used to test whether the constructor’s prototype property appears anywhere in the object’s prototype chain

P instaceof person === true, p is the instantiated object of Person, used to wrap objects or to judge objects of reference type.

var str1 = 'zenquan';
console.log(str1 instanceof String); // false
var str2 = new String('jomsou');
console.log(str2 instanceof String); // true
Copy the code

Possible interview questions: How to determine an array?

Method 1: Instanceof

arr instanceof Array
Copy the code

Method 2: array.isarray ()

Array.isArray([])
Copy the code

The typeof and Instanceof implementations are combined to determine whether the type is primitive

class PrimitiveString {
    static [Symbol.hasInstance](x) {
        return typeof(x) == 'string'; }}console.log('hello world' instanceof PrimitiveString);
Copy the code

Typeof ‘Hello world’ === ‘string’, so the result will be true. This is a reflection of the fact that Instanceof is not 100% reliable.

A method for determining data types

  1. Object.prototype.toString.call(e).slice(8, -1)
function type(e) {
    return Object.prototype.toString.call(e).slice(8.- 1);
}
console.log(type(null))
console.log(type(1))
console.log(type('zenquan'))
console.log(type(undefined))
console.log(type(Symbol()))
console.log(type(true))
console.log(type(console.log))
Copy the code
  1. Type: JS type detection library, make up for typeof problems, native compatible with IE6
  2. It can be judged by some built-in API functions of the system, eg:Array.isArray()

Therefore, typeof and Instanceof are combined to determine data types.