JS front-end interview basics-variable types and calculations


Introduce the question
   1. What types can typeof judge?
  2. When to use == and when to use ===
  3. The difference between value type and reference type
  4. Handwriting deep copy

1. JS basics

1. Value types and reference types

Value type
Insert picture description here
reference type
Insert picture description herestack analysis: (stack is from top to bottom, heap is from bottom to top)
Value type:
Insert picture description here
heap type:
Insert picture description here
summary:
common value types are: number undefined string boolean symbol (ES6)
common reference types are: Object array special reference type: null function () function

2. typeof operator

Judgment of all value types
Insert picture description here
can determine whether the function
Insert picture description here
can recognize the reference type (can not continue to identify)
Insert picture description here

3. Deep copy (emphasis)

Insert picture description here

Second, variable calculation and type conversion

1. String stitching

const a = 100 + 10    // 110  可以使用100 + parseInt(‘10’)
const b = 100 + '10'   // 10010
const c = true + '10'  // true10	
const d = 100 + parseInt('10')    // 110

2. == and ===

100 == ‘100’   // true
0 == ‘’   // true
0 == false   // true
false == ‘’   // true
null == undeined   // true
// 除了 == null 之外,其他都一律用 ===
const obj = { x: 100 }
if (obj.a == null ) {}
// 相当于
// if (obj.a === null || obj.a === undefined) {}

3. If statement and logical calculation

truly variable: !! a === true variable (true after two non-operations)

Falsely variable: !! a === false variable (false after two non-operations)

//  一下是falsely变量,除此之外都是truly变量
!! 0 === false
!! NaN === false
!! ‘’ === false
!! null === false
!! undefined === false
!! false === false

3. Question answer and summary

1. typeof can determine which types

1. Identify all value types
2. Identify function
3. Determine whether it is a reference type (not subdivided)

2. When to use == and when to use ===?

Just remember: except for == null, use ===

3. The difference between value type and reference type?

The value type is deep copy, and the reference type is shallow copy. The output of the following code can illustrate
Insert picture description here

4. Handwritten deep copy

Logical order:
1. Pay attention to judge value type and reference type
2. Pay attention to judge whether it is array or object
3. Use recursion

4. Summary

1. Value type and reference type
2. Stack model
3. Deep copy
4. typeof operator
5. Type conversion
6. Truely and falsely variable

Published 6 original articles · won 11 · views 167

Guess you like

Origin blog.csdn.net/m0_46269977/article/details/105564644