Explicit conversion

The other data types are converted to Number

  • How many ways are there to convert values of other data types to numeric values?
    • Number()
    • parseInt()
    • parseFloat()
    • +/- (Unary operator)
  • What are the similarities and differences?

Both of them can convert non-numerical values into numerical values, with the following differences:

1. Number() can be handledBoolean,null,undefined.ObjectwhileparseIntCan not be2.When processing strings, -number can process numeric strings (positive and negative), including hexadecimal digits and empty strings. -parseIntParsing starts from the first non-space character until a non-numeric string is encountered. If the first character is not a number or a minus sign,parseIntIt will returnNaN
3. parseFloat() andparseInt() similar, +/- andNumber() is similar toCopy the code
  • The rules?

All nans can be converted to numbers except in the following cases

1. BooleanValue,true--->1  false--->0
2. null--->0
3.Empty string (' ') -- - >0
4.Numeric string ('123'.'1.23'.'0xf') -- - >123.1.23.15
5.The valueOf method is called to see if the value returned by the method can be converted to a number. If not, the toString method is called based on the return value, and then the toString method is tested to see if the value returned can be converted to a numbervar a = {
        valueOf: function() {
            return 'xyxy';
        },
        toString: function() {
            return 1; }};Number(a)  // NaN
Copy the code

Other data types are converted to strings

  • How many ways are there to convert a value from another data type to a string?
    • Values for other datatype. ToString ()
    • String(value of other data types)
  • What are the similarities and differences?
nullwithundefinedThere is no toString() method onCopy the code
  • The rules?

String concatenation ({}, [], function(){}); function(){}); function(){};

{} = >"[object Object]"  
function(){} = >"function(){}"{} +0; //0 is not string concatenated because {} is treated as a block({} +0); //"[object object]0
Copy the code
// Make a problem

let result =10 + false +undefined+ + []'Tencent'+null+true+ {};//(10+0+NaN)+''+'Tencent' +'null' +'true' + '[object Object]'
//"NaNTencentnulltrue[object Object]" 
console.log(result)
Copy the code

Other data types are converted to Boolean

  • How many ways are there to convert values of other data types to Booleans?
    • ! Invert after converting to a Boolean
    • !!!!! Convert to a Boolean type
    • Boolean
  • The rules?
only0,NaN, empty string,undefined,nullThe five values are going to be zerofalseThe resttrue
Copy the code

Implicit conversion

IsNaN, Math methods

Reference explicit conversion toNumberThe rules ofCopy the code

+ operation

If one of the data on both sides is string type, string concatenation operation is used, and then the addition operation of mathematical calculation is consideredCopy the code

= =

  1. “A few special points of the same type”
{} = = {} :falseDescription: Object (reference type value) compares the address of heap memory [] == [] :falseSame as aboveNaN= =NaN : false
Copy the code

2. “Different types of conversion rules”

1. null= =undefined : true
2. null,undefinedIt's not equal to any other value that is, you can't compare equalitynullwithundefinedTo any other job3.The string == object is converted to a string4.If the data types on both sides of == are inconsistent, they need to be converted to numbers for comparison5.Note: althoughNumber(nullThe result is that0, butnulland0== comparison is not converted to numerical comparison, i.e. validation3Put it inCopy the code