Converts other types to numeric types

What happens:

1)isNaN detection:

If the value detected is not a Number, the browser will call the Number method to convert it to a Number and then check whether it is an invalid Number. For example:

      isNaN('3') = >false
Number('3') - >3
isNaN(3) - >false
      isNaN('3px') = >true
Number('3px') - >NaN
isNaN(NaN) - >true Copy the code

2) based on parseInt/parseFloat/Number is converted into digital type manually

3) Math operations +, -, *, /, %

4) Other values are sometimes converted to numeric types when comparing based on ==

Conversion regularity

Convert strings to numbers

Whenever an invalid numeric character is encountered, the result is a NaN

    ' '= >0.    ' '= >0.    '\n'= >0.    '\t'= >0.    '1'= >1. '1a' => Nan Copy the code

Converts a Boolean to a number

      0= >false
      1= >true
Copy the code

Convert null and undefined to numbers

      null= >0
      undefined= >NaN
Copy the code

Converts the reference type to a number

First to a string and then to a NumberCopy the code

Convert other types to strings

What happens

1) based on alert/confirm/prompt/document. The write method such as output, the output value is converted to a string, and then output

  alert(1) = >'1'
Copy the code

2) String concatenation based on ‘+’

  "hello" + true= > "hellotrue"
Copy the code

3) When a reference value is converted to a number, it is first converted to a string and then to a number

  var obj = {};
  console.log(obj + 1) = >"[object Object]1"
Copy the code

4) Set a property for the object. If the property name is not a string, convert it to a string first, and then store it in the object as a property

5) manually call the toString/toFixed/join/String method

  var n = Math.PI
  n.toFixed(2) = >'3.14'
  var arr = [1.2.3]
  arr.join('+') = >'1 + 2 + 3'
Copy the code

Conversion regularity

All data types except for objects are general conversions and whatever the object is, the result of toString is **[object object]**


Converts other values to Boolean types

What happens

Based on! /!!!!! / Boolean, etc

Conditions in conditional judgments are eventually converted to Boolean types

Conversion regularity

    Boolean(' '); = >false
    Boolean(0); = >false
    Boolean(null); = >false
    Boolean(undefined); = >false
    Boolean(NaN); = >false
Copy the code

All others are true


Special case: mathematical operation and string concatenation ‘+’

1. When a string appears in an expression, it is a string concatenation; otherwise, it is a mathematical operation

  1 + true= > 2
  '1' + true= > '1true'
Copy the code

2. When a reference type is converted to a number, it is first converted to a string, and finally to a string concatenation

  [12] + 10= >'1210'
  ({}) + 10= >"[object Object]10"
Copy the code

3. {} block-level scope is connected to mathematical budget

  {} + 10= >10
  [] + 10= >'10'
Copy the code

“= =” comparison

“==” During comparison, if the data types on the left and right sides are different, they are converted to the same data type before comparison

“Object == object”

If the address space is equal, the range is true

“Object == number”

Convert objects to numbers and then compare them

“Object == Boolean”

Convert both objects and booleans to numbers and compare them

“Object == string”

Converts both objects and strings to numbers

“String == number”

A string is converted to a number

“String ==bool”

All converted to numbers

“Bool = = number”

Bool is converted to numbers

    null= =undefined= >true
    null= = =undefined= >false
    null,undefinedIt's not equal to any other value    NaN= =NaN= >false

[] = =false : true ! [] = =false: true [] = =true : false ! [] = =true : false Copy the code