Data type and typeof

Time:2021-1-22

Personal study notes, please point out any mistakes

data type

Basic data type (original value type)

  • number NaN/Infinityspecial
  • stringSingle quotation mark / double quotation mark / back quotation mark“
  • boolean true/false
  • null
  • undefined
  • symbolCreate unique values
  • bigint

Reference data type:

  • object
    • {}Common objects
    • []Array object
    • /^[+-]?(?:\d|(?:[1-9]\d+))(\.\d+)?$/Regular object
    • Date object
    • function(more special,A function is a regular object with a function that can be called

There are 8 big data types

More detailed and official explanation referenceMDN: Javascript data type and data structure

be careful:
In JavaScript,setandmapIs it a data structure, or is it in essence a data structureObjecttype

typeof

typeof OperatorReturn acharacter stringThat represents the number of uncomputed operandstype

//7 basic data types
typeof 10
//"number"
typeof '1'
//"string"
typeof null
//"object"
typeof undefined
//"undefined"
typeof Symbol()
//"symbol"
typeof 9007199254740991n
//"bigint"
typeof true
//"boolean"
//Object type
typeof {}
//"object"
typeof []
//"object"
typeof /^$/
//"object"
typeof new Date()
//"object"

Pay attention to something special

//Special values to remember
typeof NaN
//"number"
typeof function(){}
//"function"
typeof null
//"object"
Typeof [] // don't confuse. Typeof can't subdivide objects
//"object"

Pay attention to three packaging objects

The so-called “wrapper object” refers to the value, string, Boolean value corresponding to eachNumberStringBooleanThree native objects. These three native objects can turn the value of the original type into (wrapping) objects.

var v1 = new Number(123);
var v2 = new String('abc');
var v3 = new Boolean(true);

typeof v1 // "object"
typeof v2 // "object"
typeof v3 // "object"

v1 === 123 // false
v2 === 'abc' // false
v3 === true // false

typeof null === 'object'reason

typeofThe principle of this method is as follows:

  • All data type values stored in the computer are stored in accordance with the “binary” storage
  • null -> 000000
  • All objects start with 000
  • When type of ‘is detected, it is detected according to the binary value stored in the computer

MDN explains:

Conclusion:

Because in JavaScript, the type of value is determined byType label + actual valueIndicates that the object’s type label is 0, andnullPointer to null, represented by the value of 0x00, sonullThe type label of is also 0, sotypeof nullreturn"object"

NaN

NaN: not a number is not a valid number, but it belongs tonumberData type

console.log(typeof NaN); //=>"number"
console.log(NaN == NaN); //=>false
console.log(NaN === NaN); //=>false
let n = 10;

//How to detect Nan
if (isNaN(n)) {
    //The condition is true: prove that it is really a non significant number
}
Object.is ([val1], [val2]) // check whether two values are equal
console.log(Object.is(NaN, NaN)); //=>true

Object.is()Internal vsNaNSpecial treatment has been made for the judgment of

Infinity

typeof Infinity//"number"
Infinity===Infinity//true
-Infinity===Infinity//false

symbol

Brief introduction

What is the symbol in JS?
ECMAScript 6 getting started – symbol

Symbol creates a unique value.

console.log(new Symbol()); //Uncaught TypeError: Symbol is not a constructor
//Cannot use new summer (base data type)
console.log(Symbol('AA') === Symbol('AA')); //false
//A unique value will be created every time. The string in brackets can be regarded as a mark and a comment, just for distinguishing

let symb = Symbol('BB');
console.log(symb === symb); //true

bigint

Maximum safe number and minimum safe number

Number.MAX_SAFE_INTEGER//9007199254740991
Number.MIN_SAFE_INTEGER//-9007199254740991
Math.pow(2,53)//9007199254740992

If the number exceeds this, there will be an error in the calculation

Bigint solves the problem of large numbers

If the server storage ID is a long integer, the int value is very large. When the server converts it into a string, there is no problem in returning it to the client. If the server returns it as a number type, then the returned ID is different in the browser and in the server, which will change and cause an error

To solve this problem, a new data type is introduced in ES6bigintAfter the numbernmeanbiginttype

9007199254740992123n+1n//9007199254740992124n
typeof 9007199254740992124n//"bigint"