The subway of this morning saw a very basic JS interview question, the result was tragic… Let’s start from the beginning with the basics of JS.

1, js data types

Primitive type – save in stack, assignment is copy variable value

Null undefined Number String Boolean symbol BigINT // Symbol is a new data type in ES6. It is designed to prevent property name conflicts and ensure that each property name in the object is unique. // Bigint indicates an arbitrarily large integerCopy the code

Object type (reference type) – saved in the heap, assignment is copy reference address

function  array  RegExp Date  Math  Error Set  Map 
Copy the code

2. Methods for verifying data types

typeof

Null is an object, but it can be used to determine if the object type is function

instanceof

Use it to determine object types

Object.prototype.toString.call()

You can detect both raw data types and object types, for example

Object.prototype.toString.call(function(){}) // [object Function]

3. How to encapsulate a function that validates a data type in actual code?

function checkDataType(obj){ const dataType = typeof obj if(dataType ! == 'object'){ return dataType } return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() } console.log(checkDataType('qinggugu')) // string console.log(checkDataType(1)) // numberCopy the code

The above verification method is not complete. Welcome to correct and supplement. Thanks ~