Front-end interview JS—JS data types and related content

Table of contents

JS data types

Basic data types:

Reference data type:

Two data storage methods:

The difference between the two data types:

How to detect data types

The difference between null and undefined


JS data types

Basic data types:

        Number, String, Boolean, null, undefined, symbol, bigint (the last two are new for ES6)

Symbol and Biglnt are new data types in ES6

  • Symbol represents a unique and immutable data type after creation. It is mainly used tosolve possibleglobal variable conflicts question
  • Biglnt is a numeric type of data that can represent integers in any precision format. Using Biglnt can safely store and operate large integers, even if this number exceeds the safe integer range that Number can represent
Reference data type:

        object,function(proto Function.prototype)

        ​​​​ Object: ordinary object, array object, regular object, date object, Math mathematical function object.

Two data storage methods:
  • Basic data type is a simple data segment directly stored instack, occupying small space and The size is fixed and is frequently used data. The stack is the space where basic type values ​​and executed code are stored.
  • Reference data type is stored inheap memory, which occupies a large space and is not fixed in size. The reference data type stores a pointer in the stack, which points to the starting address of the entity in the heap. When the interpreter looks for the reference value, it will retrieve its address in the stack, and obtain the entity from the heap after obtaining the address.
The difference between the two data types:
  1. The heap has larger space than the stack, and the stack runs faster than the heap.
  2. Heap memory is unordered storage and can be obtained directly based on references.
  3. Basic data types are relatively stable and occupy relatively small memory.
  4. Reference data type sizes are dynamic and unlimited.
How to detect data types

typeof

Arrays/objects/null will all be judged as objects, and everything else is correct.

instanceof

can only correctly determine the reference data type, cannot determine the basic data type.

The difference between null and undefined
  • Undefined and Null are both basic data types. These two basic data types have only one value, undefined and null respectively.
  • undefined means undefined, and null means an empty object. Generally, undefined will be returned when a variable is declared but not defined. Null is mainly used to assign values ​​to some variables that may return objects as initialization.
  • When using typeof to judge these two types, Null typing will return "object"
  • Returns true when using a double equal sign to compare two types of values, and returns false when using a triple equal sign.

Guess you like

Origin blog.csdn.net/qq_62799214/article/details/134758978