1. Basic data types (value types)

  • number
  • string
  • boolean
  • null
  • undefined
  • symol
  • bigint

“Basic data types are stored directly on the stack”, the browser will create a block of memory in the computer’s memory for the execution of JS code, this is the stack memory: execute the environment stack, and the basic data types are stored directly in the stack memory

2. Reference data types

  • object
  • array
  • function

Storage is in a separate block of memory: heap memoryWhen a reference data type is created, a separate Heap is created: the Heap memory in which the reference data type is stored. Each heap has a hexadecimal address, which is stored on the stack for later variable association

3. Data type conversion

1. Convert other data types to number

  • According to conversion
    • Number([value])
    • parseInt/parseFloat([value])
  • Implicit conversion (the browser converts to number by default internally)
    • isNan([value])
    • Mathematical operations (special case: string concatenation is performed when the + sign appears in a string)
    • When == is compared, some values need to be converted to number for comparison

When objects perform mathematical operations (objects are converted to numbers) : underlying mechanism

  1. Check the object’s Symbol. ToPrimitive property value, if any, based on this value
  2. If not, the valueOf valueOf() for the object is “primitive: primitive value”, and if so, the operation is performed based on this value
  3. If it is not the original value, get the object’s toString() and turn it into a string. When converted to a string, the + sign is concatenated
  4. If the last thing you want to do is convert the string to a number

let obj = {};

console.log(20 + obj); // '20[object Object]'



{} + 20 // 20, because the previous empty object will be parsed into code blocks when parsing

Copy the code

/ / Number mechanism

console.log(Number('0')) / / 0

console.log(Number('10')) / / 10

console.log(Number('1abc')) // NaN is used whenever an insignificant number occurs

console.log(Number(null)) / / 0

console.log(Number(undefined)) // NaN

console.log(Number(false)) // 0; otherwise true is 1



// Convert the object to a numeric type, as described above



// parseInt:

// Look for valid numeric characters from the left side of the string

// Stop the search whenever an invalid numeric character is encountered

// parseFloat identifies one more decimal point

parseInt(' '// NaN

Number(' ')   / / 0

isNaN(' ')    // false if Number('') is set to 0 before isNaN

parseInt(null// NaN, you need to convert null to a string first

parseInt('10abc'/ / 10

parseFloat('1.2 ab) + parseInt('1.8 ABC'/ / 1.2 + 1

Copy the code

2. Other data types are converted to strings

  • A method that can be used directly
    • toString()
    • String()
  • Implicit conversion
    • String concatenation occurs when a string appears on one side of a plus sign operation
    • toString()

  1. It is easy to convert other data types to strings, usually by enclosing them in quotes.
  2. Only {} ordinary objects call toString(). Instead of converting to a string, toString() is typed and returns: “[object object]”

3. Convert other data types to Boolean

  • Transformation way
    • ! Invert after converting Boolean values
    • !!!!! Convert to a Boolean type
    • Boolean([val])
  • Implicit conversion
    • In condition judgment, the result of condition processing is Boolean type value

Rule: Only 0, NaN, null, undefined, and empty string will become Boolean false, the rest will be true

4. == comparison, data conversion rules

  • Same data type on both sides (special case)
    • {}=={} : false, the reference type compares the heap memory address
    • []==[] :false for the same reason as above
    • NaN==NaN:false
  • == the two sides are different data types
    • Null ==undefined: true; otherwise, null/undefined is not equal to any other data type
    • Object == string, the object is converted to a string before comparison
    • The remaining == two data types are different, need to convert to number type before comparison