1.JS data type

1.1 Original Type:

Null: null;

Undefined: undefined;

Booolean: true,false;

Number: integer or floating point Number, special value (Infinity,NaN);

String: String.

Symbol: unique and immutable data type;

BigInt: The number that can be manipulated exceeds the maximum security number.

1.2 Reference Types

Object: the Object, array, function and so on

2. The difference between primitive and reference types

2.1 Immutability

The primitive type itself cannot be changed. For example, no method that operates on a string can change the string directly, but creates a new string based on the original string.

Js data memory space is divided into two types, stack memory and heap memory

  • Stack memory:

  • Values stored are fixed in size

  • The space is lesser

  • Can directly operate its saved variables, high operation efficiency

  • The system automatically allocates storage space.

The value of a variable of primitive type is stored directly in the stack, which allocates memory for the variable when it is defined. Since the size of memory space in the stack is fixed, variables stored in the stack are immutable.

2.2 variability

  • Heap memory:

  • Stored values vary in size and can be adjusted dynamically

  • Large space, low operating efficiency

  • Unable to manipulate its internal storage directly, read using a reference address

  • Space is allocated through code

The value of a reference type is actually stored in heap memory, and it stores only a fixed-length address on the stack that points to the value in heap memory. So it can be easily changed.

2.3 copy

When a variable is copied to another variable, the original and reference types behave differently.

Primitive type copy: Two variables have the same value, but they point to completely different memory Spaces. The participation of two variables in any operation does not affect each other.

Reference type replication: Essentially copying addresses stored in the stack, both variables refer to the same object in heap memory, so changing the value of either variable affects the other. That’s why there are light and dark copies.

2.4 compare

Primitive type: Compares values directly, returning true if they are equal

Reference type: Compares their reference addresses, even if the addresses point to the same value but are stored in different storage Spaces. Returns false

2.5 Value passing and Reference Passing

Function parameters are passed by value, that is, a parameter is only a local variable, changing the local variable does not affect the external variable.

When a function parameter is a reference type, a copy of the parameter is also copied to the local variable, but the copy also points to the address in the heap memory. Inside the function, the operation on the object’s properties is the same as outside the function, operating on the same address.

3. The null, and undefined

Null: Indicates that an object has been assigned a value, indicating that it is null and should have no value. 0 when converted to a number.

Undefined: indicates that a value is missing. There should be a value here, but it has not been defined. NaN when converted to a number. The value of an attribute of the object should not be undefined.

Js is a dynamically typed language. Members may not exist except for null values. Whether they exist or not is only known at runtime. That’s what undefined means.

4.Number

4.1 Accuracy loss 0.1 + 0.2! = 0.3

The calculation involves converting the data to binary for calculation and then converting the result to decimal.

Binary error occurs at 0.1 + 0.2

4.2 STORAGE mode of JS to binary

The binary of decimals is mostly an infinite loop, represented by a fixed length of 64 bits

In 64-bit, the sign bit 1; Index bit 11; Mantissa 52; (Scientific notation)

Therefore, digits after the 53th significant digit cannot be stored, following the principle of carrying 1 and discarding 0

So 0.1 and 0.2 have lost accuracy

4.3 Maximum security number

The maximum exponent digit is 1023, and the mantissa digit is 1, which is the maximum Number

Number.Max_Safe_Integer represents the maximum safe Number within which no loss of precision occurs (except for decimals)

After ES10, you can use BigInt to manipulate numbers that exceed the maximum safe number

5. Special reference types

5.1 Packaging Type (Basic Packaging Type)

  • Boolean
  • Number
  • String

Tyoeof new Returns object

The difference between a reference type and a wrapper type is the lifetime of the object. Instances of the type created using the new operator remain in memory until the execution stream leaves the current scope, whereas primitive types exist only for the moment of execution of a line of code and are then destroyed immediately.

5.2 Packing and unpacking

Boxing: Converts the base type to the corresponding packaging type

Unboxing: Converts a reference type to a primitive type

When we operate on an underlying type, an object wrapped around the type is automatically created in the background, allowing us to call some methods and properties.

During unpacking, toPrimitive principles are followed, that is, valueOf and toString methods are called in different order.

  • When a reference type is converted to Number, valueOf is called, followed by toString
  • When a reference type is converted toString, toString is called, followed by valueOf

6. Type conversion

6.1 If and logical Statements

  • null

  • undefine

  • ‘ ‘

  • NaN

  • 0

  • False

Will be converted to false and the rest to true

6.2 = =

When == is used, if both sides of the type are the same, the comparison result is the same as ===, otherwise an implicit conversion will occur. The conversion that occurs when == is used can be divided into several different cases (considering only the two sides of the type).

  • NaN always returns false compared to any other type, including itself

  • Boolean compares, first converts to a number that is 0 or 1

Boolean comparisons with undefined and null return false because Boolean is converted to 0 first

  • String is compared to number. String is converted to a number

  • Null == undefined returns true, otherwise null and undefined and all other comparisons return false

  • A primitive type is compared to a reference type and is unboxed into two primitive types

Array elements that are null or undefined are treated as empty strings and converted to 0

6.3 How to make if(a == 1&&a == 2) true

var a = { value: 0, valueOf: function() { this.value++; return this.value; }}; console.log(a == 1 && a == 2); //true a is implicitly converted, incremented each time a is compared, so returns trueCopy the code

7.JS method to judge the type

7.1 typeof

Typeof can be used for primitive types and functions, but not for reference types, because object is returned

7.2 the toString

The toString function is called to unbox and convert to a primitive type.

7.3 summarize

Original type use typeof (), with the Object reference type. Protorype. ToString. Call ()