The data type
– Basic data type
Number,String,Boolean,Undefined,Null,Symbol
Copy the code
– Reference data type
Array,Function,Object
Copy the code
Data type judgment
– typeOf: Determines whether the data is a basic data type
'typeof A === 'undefined' :a undefined' typeof A === 'number' : A is numeric' typeof A === 'string' : A is string' typeof A === 'bollean' ': A is muppet type 'typeof a === 'function'' : A is function method 'typeof a === 'object'' : A is null, array or objectCopy the code
– instanceof: used to check reference types, Array and RegExp, but not Function
`[] instanceof Array`: true
`[] instanceof Object`: true
`{} instanceof Object`: true
`() => {} instanceof Object`: true
`() => {} instanceof Function`: true
`/\d/ instanceof RegExp`: true
`'' instanceof String`: false
`1 instacneof Number`: false
Copy the code
– Object. The prototype. ToString. Call () : is a primary Object of the prototype extension functions, used to accurately distinguish between data types
```
const type = Object.prototype.toString
type.call(''): object String
type.call([]): object Array
type.call({}): object Object
type.call(false): object Boolean
type.call(null): object Null
type.call(undefined): object Undefined
type.call(function(){}): object Function
```
Copy the code
Question 1: How does JS determine arrays and objects
1.a instanceof array
[] instanceof Array
: true;
{} instanceof Array
: false;
[] instanceof Object: true;
2. Typeof and isNaN are combined to determine
!!!!!!!!! The length attribute of an array is of type Number, and the length attribute of an object is of type Undefined
Const a = [1, 2, 3]; const b = {a: 1, b: 2, c: 3}; typeof a && ! isNaN(a.length): true typeof b && ! isNaN(b.length): falseCopy the code
3.Object.prototype.toString.call();
Object.prototype.toString.call(a): [object Array]
Object.prototype.toString.call(b): [object object]
Copy the code