JS interview-variable types and calculation
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
reference type
stack analysis: (stack is from top to bottom, heap is from bottom to top)
Value type:
heap type:
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
can determine whether the function
can recognize the reference type (can not continue to identify)
3. Deep copy (emphasis)
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
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