JS implicit conversion, explicit conversion

 

Generally, valueOf is called first when converting non-basic types. If valueOf cannot return the value of the basic type, toString is called

Strings and numbers

  • "+" operator, if one of them is a string, then all are converted to a string and then perform string splicing
  • "-" operator, conversion to number, subtraction (-a, a * 1 a/1) can be implicitly coerced
[] + {}  // "[object Object]"

{} + []  // 0 

Boolean to number

  • 1 + true = 2
  • 1 + false = 1

Convert to boolean

  • second in for
  • while
  • if
  • Ternary expression
  • || (logical OR) && (logical AND) operand on the left

symbol

  • Cannot be converted to numbers
  • Can be converted to boolean values ​​(both are true)
  • Can be converted into the string "Symbol(cool)"

Relaxed equality and strict equality

Relaxed equality allows forced type conversion, while strict equality does not allow

Strings and numbers are converted to numbers and then compared

Other types and Boolean types

  • First convert the Boolean type to a number, and then continue to compare

Object and non-object

  • Execute the ToPrimitive of the object (object) and then continue to compare

List of false values

  • undefined
  • null
  • false
  • +0, -0, NaN
  • ""

Guess you like

Origin blog.csdn.net/lianjiuxiao/article/details/114883634