Study and recite the whole text

The original value

type Undefined Null String Boolean Number
value undefined null All strings true false All numbers /NaN

Reference types

The members of an Object are called objects, including Array, Function, Math, Date, JSON, RegExp, and so on.

valueOf / toString

Object String Array Number Date Boolean Function
valueOf The original value The original value The original value The original value Millisecond timestamp The original value string
toString [Object Object] The original value The result returned by the join method Numeric string Local time string ‘true’/’false’ string

Boolean(x)

X Undefined Null String Boolean Number Object
Boolean(X) = true Non-empty string true Non-zero NaN all
Boolean(X) = false undefined null An empty string false 0 NaN

Number(x)

X Undefined Null String Boolean Number Object
Number(X) = number All for the digital The same return Desc: call valueOf first and then convert to Number
Number(X) = 0 null ‘0’ false 0
Number(X) = 1 ‘1’ true 1
Number(X) = NaN undefined Contains characters other than digits NaN

String(x)

X Undefined Null String Boolean Number Object
String(x) ‘undefined’ ‘null’ The same return ‘true’/’false’ The toString results First to the original value, then toString is called

Object(x)

x undefined null Boolean Number String Object
Object(x) {} {} Boolean object whose original value is X The Number object whose original value is X A String whose original value is X Original value of the output
Object(undefined) / / {}
Object(null)  / / {}
Object(true)   // Boolean {true}
Object(false)  // Boolean {false}
Object(1)      // Number {1}
Object('hello') // String {"hello"}
Object({ a: 1 }) // {a: 1}
Copy the code

scenario

Is converted to a Number

  • Unary +, – operator
+ {} =NaN
+'20' = 20
-'20' = - 20+ [] =0; + [1] = 1; + [1.2] = NaN
Copy the code
  • The ++, — operator

  • +, -, *, /, %

Note that the + evaluation is converted to a string when a string is involved in the evaluation.

  • >, <, >=, <= comparison operators, operands are not String

  • = =,! The = operands have only String and Number, or either of them is Boolean

Converted to Boolean

  • &&! | | operation
  • Conditions of operation
  • if, while, do-while, for

Convert a String

  • The + sign operator, one of which is a string
  • Comparison operations, where one of the operators is a string