The data type

Before the es6

Basic data types: Number, String, Boolean, Null, undefined

Reference data type: Object, including Function, Array, and Date

Es6 new

Symbol、BigInt、Map、Set、WeekMap、WeekSet

Determine the data type

Use Typeof to determine the underlying data type

typeof(1)                              //number
typeof("1")                            //string
typeof(true)                           //boolean
typeof(undefined)                      //undefined
typeof(null)                           //object
typeof([])                             //object
typeof({})                             //object
typeof(function () {})//function
typeof (BigInt('1'))                   //bigint
typeof (1n)                            //bigint
Copy the code

The method used here is to use the instanceof attribute to determine whether the stereotype of the data is the specified data type, mainly the reference data type

var obj = {};
obj instanceof Object;           //true

var arr = [];
arr instanceof Array;           //true

var now = new Date(a); nowinstanceof Date;             //true

var func = function(){};
func instanceof Function;        //true

var str = new Date(a); strinstanceof Date;            //true

var str = "string";
str instanceof String;           //false
Copy the code

You can also use the Prototype property to get the object’s prototype

Object.prototype.toString.call(data):
//[object String] A character String
/ / / the object Number] Number
//[object Boolean] Specifies a Boolean value
//[object Undefined] undefined
//[object Null] null
/ / / the object Array] Array
/ / / the object Function] Function
/ / [object object] object
//[object RegExp] Regular expression
//[object Date] Date object
Copy the code

Considerations for determining data types

  1. When typeOF is used, both Array and Object data types are Object types.
  2. When typeof is used, the null type is also considered an Object type;
  3. Data of type BigInt can be declared by adding n(for example, 1n) after a number. Therefore, data of type BigInt can be judged as BigInt.
  4. Instanceof attribute, mainly through the prototype chain to determine the data type, so only reference type data judgment will be successful;
  5. Using these methods +new Date(), (new Date()).valueof (), and Number(new Date()), you can obtain the same timestamp as new Date().getTime().
  6. A Map is an array of [key,value] key-value pairs.
  7. A Set is an array and does not allow duplicate values in the array if the object is unrecognizable.
  8. WeakMap key can only be Object type.
  9. WeakSet can only be a collection of Object objects, but not any arbitrary value of any type;