Relearn Javascript's type conversion

Relearn Javascript's type conversion

Force type conversion (display type conversion)

  • Number()
  • Boolean()
  • String()

Calling valueOf() on arrays, functions, objects, and regular expressions will return the object itself. The valueOf() method defined by the date class returns its internal representation: the number of milliseconds since January 1, 1970.

Automatic type conversion

Plus (+) operator

-As a unary operator: often used to convert operands to numeric values
-As a binary operator
    -If one of them is an object, the object will be converted to the original type. Date objects are converted by calling toString(), and others are converted by valueOf() (**if valueOf() returns a value of the original type**). Since most objects do not have a usable valueOf() method, they will be converted through toString().
    -After the object is converted into a primitive type value, if one of the operands is a string, the other operand must also be converted into a string, and then the string is spliced.
    -Otherwise, both numbers are converted to numbers (or NaN), and then the addition operation is performed.

Two examples

There is a very interesting one, which cannot be understood by the above rules: {} + {}: NaN (actually in the Chrome console, the output is: "object Object") {} + []: 0

This is because the browser will parse the first {} as an empty code block, so the above two are equivalent to: + {}, + [] Therefore, the corresponding results above are obtained.

Here add a conversion process of these two: As mentioned above, they are equivalent to + {} and + [] respectively. Therefore, as a unary operator, + must convert the following {} and [] to integers. This conversion is equivalent to Number({}) and Number([]). At the very beginning, we introduced the principle of conversion through Number():

  • First call the valueOf() method. For an object, the valueOf() method returns itself.
    image.png
  • Since the above returned is not a basic type, call the toString() method. Return "object Object" and "" respectively. This is a basic type (String). So call Number() on them respectively, the converted values ​​are: NaN and 0

== operator

  • If one is null and one is undefined, return true
  • If one is a number and the other is a string, convert the string to a number and then compare.
  • If a value is a boolean, convert it to a number
  • If one value is an object and the other is a value or a string, first convert the object to a primitive type through valueOf() or toString(), and then compare. ( JS core built-in classes will first try valueOf(), and then try toString(). Except for date classes, date classes only use toString() conversion. )
  • Does not meet the above, all are false

Note here: In the implicit conversion performed by + or ==, objects (except date types) are first attempted to use valueOf() for conversion, and then try to use toString().

Reference: https://cloud.tencent.com/developer/article/1668315 Relearn Javascript Type Conversion-Cloud + Community-Tencent Cloud