preface
This article has sorted out the explicit and implicit conversions in JavaScript. If you have a different opinion about the answer, you are welcome to add to the discussion in the comments section.
Test it. If you know it, you don’t have to look downNo, it won’t.
Explicit type conversion
1, convert to Number type:
Number()
/ parseFloat()
/ parseInt()
String([1.2.3]) / / "1, 2, 3"
String([]) // "" is an empty string
String({}) //"[object Object]"
Copy the code
2. Convert to String:
String()
/ toString()
// String to number: The empty string becomes 0, and if any non-valid numeric character occurs, the result is NaN
Number("") / / 0
Number("10px") //NaN
Number("10") / / 10
// Boolean converts to a number
Number(true) / / 1
Number(false) / / 0
//null and undefined are converted to numbers
Number(null) / / 0
Number(undefined) //NaN
//Symbol cannot be converted to numbers
/ / BigInt get rid of "n"
Number(12312412321312312n) / / 12312412321312312
// Converting objects to numbers --------- is more complicated
let obj ={name:'xxx'}
console.log(obj-10) // Math: convert obj implicitly to a number first, then perform the math
// Running mechanism
obj[Symbol.toPrimitive] // UNdipay invokes the symbol. toPrimitive method of the object first, if it does not exist
obj.valueof() // {name: XXX} then call valueOf of the object to get the original value, if the value is not the original value
obj.toString() // [object object] Calls the toString of the object to turn it into a string
Number ("[object object]") // NaN finally converts the string to a Number using the Number() method
NaN-10 // NaN
Copy the code
Type Boolean;
Boolean("1") // true
Boolean("") // true
Boolean("") // false Notice the difference
Boolean(0) // Only 0 is false
Copy the code
Implicit conversion
Rule: make sure that the operation is as error-free as possible, and then convert as much as possible to strings without making mistakes
Implicit conversions of the 1, +, -, *, \ operators
Except for the + sign, all other operators are converted to Number if possible
+ operator priority conversion order, Boolean —– number —— string, such as
let result = 100 + true + 21.2 + null + undefined + "Tencent"+ + []null + 9 + false;
// The result should be?
console.log(result) //NaNTencentnull9false
// 1. First 100 + true
// if the Number type exists on both sides of the + concatenation, the Number will be 1 if the Number is true, and the result is 101
/ / 2.101 + 21.2
// add the Number on both sides of the + concatenation to 122.2
/ / 3.122.2 + null
If the Number type exists on both sides of the + concatenation, when null, the Number is 0, and the result is 122.2
/ / 4.122.2 + undefined
// there is a Number type on both sides of the + concatenation, where undefined converts Number to NaN. NaN and any data type are calculated as NaN, and the result is NaN
// 5.NaN + "Tencent"
// add String type NaN to String "NaN" and result: "NaNTencent"
// 6."NaNTencent" + []
// + String type exists on both sides of the concatenator. [] convert String to "" and concatenate String. Result: "NaNTencent"
// 7."NaNTencent" + null
NaNTencentnull = NaNTencentnull = NaNTencentnull = NaNTencentnull
// 8."NaNTencentnull" + 9
// + String concatenation: NaNTencentnull9 = NaNTencentnull9
// 9."NaNTencentnull9" + false
// + String concatenation: "NaNTencentnull9false"
Copy the code
2. Implicit conversion on both sides of operator ==
If the two data types are different, convert them to the same type and then compare them
[1.2.3] = ='1, 2, 3' //true
null= =undefined //true= = {} {}// the false object == object compares the heap memory address, the same address is equal
console.log([1.2.3] = = [1.2.3]) //falseExcept for the above cases, if the two types do not match, the rest will be converted to numbers, such as:'123'= =123 / / true Number (' 123 ') = = = 123
Copy the code
Third, pay attention to
console.log("true"= =true) // False should be the right side of the true is converted to the number 1, then 1 is converted to the string '1'
console.log(["x"] = ="x") //true
console.log("!!!!! false"= ="!!!!! true") //true
console.log([] + null + 1) //null1
console.log(new Date(0) + 0) Thu Jan 01 1970 08:00:00 GMT+0800 (Chinese Standard time)0
console.log(new Date(0) - 0) // 0
// Have fun
console.log((! + + [] [] +! []))//"truefalse"
/ /! + [] +! [] ---- true + [] + ! [] ---- "true" + ! [] ---"true"+!" " --- "true"+"false"
Copy the code
There are two special rules to keep in mind:
- No numeric conversion occurs when == is applied to null or undefined. Null equals only null or undefined, not anything else.
null= =0 // false, null is not converted to 0
null= =null // true
undefined= =undefined // true
null= =undefined // true
undefined= =0 // false
Copy the code
- NaN is not equal to any value, including itself
NaN= =NaN // false
Copy the code
conclusion
Feel good writing, helpful to you, you can share with people around you, the more you share knowledge, do not be stingy
Follow-up update front-end other small knowledge, please pay attention to me, tidy up, share with you, we learn front-end **