current position:Home>[front end interview question] es variable type (type classification, type judgment, value type and reference type)

[front end interview question] es variable type (type classification, type judgment, value type and reference type)

2022-01-27 03:34:09 Erlin loves meat

Variable type

The variable type part is mainly divided into two points :

  • JS Data type classification and type judgment
  • Value type and reference type

data type

ES It defines 6 Primitive types :

  • boolean
  • string
  • number
  • null
  • undefined
  • symbol

JS As defined in 8 Type of data :

  • boolean
  • string
  • number
  • null
  • undefined
  • object
  • symbol
  • bigint

among Symbol and BigInt yes ES6 New data types .

Symbol It mainly solves the problem of possible global variable conflict .

BigInt It's a digital type of data , It can represent an integer of any precision , We use bigint It can safely store and operate integers .

Type judgment

How to determine the type of data :

  • typeof
  • instanceof

typeof The resulting value may be Undefined、boolean、number、string、Object、function、Symbol

It should be noted that :typeof(null)、typeof(array) All the results are object.

instanceof You can solve it typeof Can't judge the problem of array , however instanceof There are also shortcomings. , Can't distinguish null and Undefined.

It was written before js Several ways to determine the type of data , There is a detailed explanation , The attached code , Interested readers can have a look :

【 Front end interview questions 】 How to judge data type ?_ Erlin loves meat blog -CSDN Blog

  Value type and reference type

according to JavaScript The transfer method of variables in , Variable types are divided into value types ( Also called basic type ) And reference types .

Value types include :

  • boolean
  • string
  • number
  • undefined
  • null

Reference types include :

  • date
  • array
  • function
  • object
  • regexp
  • error

There is a simpler classification of reference types : Array 、 object 、 function

Difference between value type and reference type :

Different storage locations : Value types are stored in the stack , The address of the reference type exists in the stack , Values are stored in the heap .

Is the value variable : Value type ( Basic types ) Once the value of the variable is determined, it is immutable , But the value of the reference type is variable .

copyright notice
author[Erlin loves meat],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270334047524.html

Random recommended