Implicit type conversions make code more opaque, but they also reduce redundancy and make code more concise
- Implicit type conversion between strings and numbers
const a = "32";
const b = 54;
// a + a = "3232";
// a + b = "3254";
// b + d = 57;
const arr1 = [1.2];
const arr2 = [3.4];
// arr1 + arr2 = "1,23,4";
Copy the code
- According to the ES5 specification, if an operand is a string or can be converted to a string, the + operator concatenates the array. ValueOf () cannot get a primitive value, so it calls toString() instead, so the two arrays above become “1, 2” and “3, 4”. + splice to “1, 23, 4”;
- The operator forces a string to be converted to a number, because numbers are used for this operator. Arrays are first converted to a string by calling toString(), and then to a number.
const a = "3.14";
const b = a - 0;
console.log(b); / / 3.14
const c = [4];
const d = [2];
console.log(c - d); / / 2
Copy the code
- Implicit type conversion of Boolean values to numbers
// Check if only one parameter is true
function onlyOne() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
if (arguments[i]) {
sum += arguments[i];
console.log(sum); }}return sum == 1;
}
Copy the code
- Implicit type conversion to a Boolean value
- conditional
- Services variable, judge condition is false, truly variable, judge condition is true
- The following are Services variables and everything else are TRULY variables
0 (+ 0, 0)
NaN
""
null
undefined
false
- Conditional statement
- A conditional expression in an if() statement
- A conditional expression in a for statement
- A conditional expression in a while loop
- A conditional judgment expression in a ternary operator
- Logic operation method, | |, & as a part of the conditional expression
- An equality comparison between null and undefined
- Null == undefined (true); null == undefined (true);
const a = null;
const b;
a == b; // true
b == null // true
a == false; //false
b == false; // false
a == ""; // false
b == ""; //false
a == 0; // false
b == 0; // false
Copy the code
- Equality comparison between objects and non-objects
-
Comparisons between objects (objects, functions, arrays) and primitive types (strings, numbers, Booleans)
(1) If Type(x) is a string or array and Type(y) is an object, the result of x==ToPrimitive(y) is a string or array
const a = 42;
const b = [42];
a == b; // true
// [42] first calls the ToPrimitive abstract operation, which returns "42", which becomes "42" == 42, and then 42 == 42
Copy the code
- “Unpacking” encapsulates an object such as (new String(” ABC “)) and returns its base type “ABC”. This also happens with ToPrimitive casts in ==
const a = null;
const b = Object(a);
a == b; // false
a === b; // false
Copy the code
- Some values are not because of other higher-priority rules in the == algorithm
const a = null;
const b = Object(a);
a == b; //false
const c = undefined;
const d = Object(c);
c == d; // false
const e = NaN;
const f = Object(e);
e == f; // false
Object(null) and Object() both return a regular Object
// NaN can encapsulate as a numeric object, but NaN! =NaN
Copy the code
- It’s a rare situation
- Return other numbers
Number.prototype.valueOf = function() {
return 3;
}
new Number(2) = =3; // true
// 2 == 3 does not have this problem, while Number(2) involves ToPrimitive conversion
Copy the code
- If I make a == 2 and a == 3 both true
let i = 2;
Number.prototype.valueOf = function() {
return i++;
}
let a = new Number(454);
if (a == 2 && a == 3) {
console.log("good!"); // good
}
Copy the code
- Equivalence comparison of false values (prone to confusion)
console.log("0"= =false); // true
console.log(0= =false); // true
console.log([] == false); // true
console.log(""= =false); // true
console.log(0= =""); // true
console.log(0= = []);// true
console.log(""= = []);// true
// Extreme case[] = =! []// true
[] ==! [] becomes [] == false
Copy the code
Array ToPrimitive is converted to characters, [null] is converted directly to “”
2= = [2]; //true
""= = [null]; //true
0= ="\n"; //true
Copy the code
-
Use implicit type conversions safely
(1) Do not use == if either side has true or false. (2) Do not use == if either side has [],””, or 0
-
Comparison of abstract relations
const a = ["123"];
const b = ["21"];
console.log(a < b); // true
const c = ["123"];
const d = 21;
console.log(c < d); // false
// ToPrimitive is called first for both sides of the comparison. If the result is not a string, the two sides are converted to a number for comparison according to ToNumber's rule. If both are strings, the character encoding is compared from scratch.
Copy the code
const a = {b:42}
const b = {b:43}
// Because a is converted to a string is [object object] b is [object object]
console.log(a<b); //false
console.log(a==b); //false
console.log(a>b); //false
// The following two cases are special
console.log(a>=b); //true
console.log(a<=b); // true
// True because in JavaScript <= should mean no greater than, >= should mean no less than
// a<=b (a> B) In the same way, a>= B is treated as! (a)
Copy the code