Data type and typeof


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 OperatorReturn acharacter stringThat represents the number of uncomputed operandstype

//7 basic data types
typeof 10
typeof '1'
typeof null
typeof undefined
typeof Symbol()
typeof 9007199254740991n
typeof true
//Object type
typeof {}
typeof []
typeof /^$/
typeof new Date()

Pay attention to something special

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

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:


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: 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
} ([val1], [val2]) // check whether two values are equal
console.log(, NaN)); //=>true vsNaNSpecial treatment has been made for the judgment of


typeof Infinity//"number"


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


Maximum safe number and minimum safe number


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

typeof 9007199254740992124n//"bigint"