Value type and reference type

For performance purposes, reference types can be large, and storage replication operations are expensive, so addresses are stored on the stack and real data is stored on the heap

Value type (base type)

It’s stored on the stack and takes up a fixed amount of space

string number boolean symbol undefined

When a method is executed, each method creates its own memory stack, which is destroyed at the end of execution.

Reference types

Stored in the heap, the stack holds the address of the value of this variable

Object array null (null is a special reference type, pointer to an empty address) function (function is a special reference type, but does not store data, so there is no copy function)Objects in the heap will not be destroyed at the end of the function execution because the object may be referenced by other variables. Only when the object is not referenced by any variables will the JS recycle mechanism destroy the object in the heap

typeof

Recognize all value types string Number Boolean symbol undefined

Identification function

typeof  console.log  // function
Copy the code

Identify reference types (not subdivided) Identify all array null objects as objects

typeof  null  // object
Copy the code

Variable calculation – type conversion

String splicing
const a = 100 + 10; // 110 const b = 100 + `10`; // '10010' const c = true + '10 '; // 'true10'Copy the code
= =

The converted value

100 == '100' // true 0 == '0' == false false ==' null '== undefined // const obj = {a: 100} the if (obj. A = = null) {} / / equivalent to the if (obj. A = = = null | | obj. A = = = undefined) {}Copy the code
If statements and logical operations

Truly variable!!!!! A === true

Falsely variable!!!!! A === false variable

The following are Services variables and everything else are TRULY variables!! false === false !! '' === false !! undefined === false !! null === false !! NaN === false !! 0=== false // Truly variable const A = true if(a){} const b = 100 if(b){} // Service variable const C = '' if(c){} const d = null if(d){} let e if(e){}Copy the code

logic

10&&0 // 0 ''||'abc' // 'abc' ! window.abc // trueCopy the code
/ / deep copy const a = {name: 'mh, age:' 21 '} function deepClone (obj) {if (obj = = null | | typeof obj! = 'object'){return obj // !!!! } let result; if(obj instanceof Array){ result = [] }else{ result = {} } for(let key in obj){ if(obj.hasOwnProperty(key)){ result[key]  = deepClone(obj[key]) } } return result } const b = deepClone(a); a.name = 'ww' console.log('b,,,,,,',b);Copy the code