The previous article focused on the rules for converting data types to Boolean,Number, and String. There is deliberately no mention of type conversions related to operators. This article will focus on this aspect of knowledge.
Pictured above,
The unary operator +
Unary operators follow the other type ToNumber rule (ToNumber).
First check if it is a basic data type. If it is
Add Symbol and BigInt
Neither of these data types supports implicit conversions and an error will be reported.
For reference data type use toptimworkouts (obj,Number).
- If the object has a valueOf method and returns a raw value, JavaScript converts the raw value to a number and returns the number
- Otherwise, if the object has
toString
Method, and returns a raw value, JavaScript converts it and returns it. - Otherwise, JavaScript throws a type error exception
The code examples
console.log(+1); //1 console.log(+"a"); //NaN console.log(+true); // 1 console.log(+false); // 0 console.log(+null); // 0 console.log(+undefined); // NaN console.log(+2n); //error console.log(+Symbol(1)); //error console.log(+[]); / / 0 console. The log (+ / 2 and 4); //NaN console.log(+{}); //NaNCopy the code
Analyze why +[2,3,4] is NaN.
- First of all, arrays are not basic data types
ToPtimitive(obj,Number)
This way. - [2,3,4] has a valueOf method but does not return the original data type, so use
toString
, returns “1,2,3”. - Error when converting “1,2,3” to a number.
The binary operator +
When calculating value1 + Value2, the rules are as follows
- lprim = ToPrimitive(value1)
- rprim = ToPrimitive(value2)
- If lprim is a string or rprim is a string, then return the concatenation of ToString(lPrim) and ToString(rPrim)
- Otherwise, return the results of ToNumber(lprim) and ToNumber(rprim)
The text is difficult to understand, let’s use the code to show a few examples.
Add the same data types
console.log(1 + 1); //2 console.log("a" + "a"); // "aa" console.log(true + true); //2 console.log(null + null); //0 console.log(undefined + undefined); //NaN console.log(Symbol(1) + Symbol(2)); //error console.log(2n + 3n); //5n console.log(NaN + NaN); //NaN console.log({} + {}); // [object Object][object Object] console.log([] + []); / / ""Copy the code
Different types of addition
True (Boolean) + other
console.log(true + "a"); // "truea" console.log(true + true); // 2 console.log(true + false); // 1 console.log(true + null); // 1 console.log(true + undefined); // NaN console.log(true + Symbol(1)); // error console.log(true + 2n); // error console.log(true + []); // "true" console.log(true + [1, 2, 3]); // "true1,2,3" console.log(true + {}); // "true[object Object] "Copy the code
“A” (String) + other
console.log("a" + "a"); // "aa" console.log("a" + true); //"atrue" console.log("a" + false); //"afalse" console.log("a" + null); // "anull" console.log("a" + undefined); // "aundefined" console.log("a" + Symbol(1)); // error console.log("a" + 2n); // "a2" console.log("a" + []); // "a" console.log("a" + [1, 2, 3]); //"a1,2,3 "console.log("a" + {}); // "a[object Object] "Copy the code
1 + other (Number)
console.log(1 + "a"); // "1a" console.log(1 + true); // 2 console.log(1 + false); // 1 console.log(1 + null); // 1 console.log(1 + undefined); //NaN console.log(1 + Symbol(1)); // error console.log(1 + 2n); //error console.log(1 + []); // "1" console.log(1 + [1, 2, 3]); / / "11, 2, 3" is the console. The log (1 + {}); // "1[object Object]"Copy the code
As you can see, the sum of these different types is easy to forget if you just try to memorize them by memory.
But if we use the rules to solve these problems, it is easy.
Here’s a look at some of the more obscure outputs. In addition of the same type
console.log(undefined + undefined); //NaN console.log({} + {}); // [object Object][object Object] console.log([] + []); / / ""Copy the code
Just use the rules.
undefined + undefined
-
Both left and right are basic data types, so lprim = undefined, rprim = undefined.
-
Neither is a string, so the result of ToNumber(lprim) and ToNumber(rprim) are returned
-
What is ToNumber(undefined)? Looking at the ToNumber rule, it’s NaN
-
The end result of NaN + NaN is NaN
{} + {}
-
{} is a reference type, so ToPrimitive(obj,Number) is used.
-
ToString returns “[object object]”, which is a string type.
-
Returns the concatenation of ToString(lprim) and ToString(rprim).
-
“[object object]” + “[object object]” The end result is “[object object][object object]”
[] + [] That’s for the gentlemen.
There are other types of conversions for the binary operator +, which readers can try if they have time. If you have any questions, please leave them at the bottom of this article and I’ll ask you for answers. If I can’t answer them, I’ll try to answer them for you.
It is also important to note that the Symbol type cannot be used for computation. BigInt can only be evaluated against itself.
= = equal
To be honest, there are too many rules for type conversions and they are not very standard, so I recommend using the more standard and strict === operator instead.
However, in case some readers may be obsessive-compulsive, here are some specific rules.
“==” is used to compare whether two values are equal. A conversion occurs when the two values to be compared are of different types.
For details on how to use “==” for comparisons, see specification 11.9.5:
When x == y:
Return false for null and undefined. Return true for String and Number. If yes, convert String to Number. If one of them is Object and the other is String, Number, or Symbol, the Object is converted to a String and the comparison is made
Oh, my god, that’s too much.
= = = be equal
=== the judgment of equality is too much simplified compared with ==, to be specific, just one.
Return true if the left and right types and values are exactly the same; otherwise return false.
However, === has two bugs, which also exist in ==
- === thinks that NaN is not equal to itself, but this does not conform to normal human logic.
- And then === thinks that +0 is exactly the same as -0.
console.log(NaN === NaN) // false
console.log(+0 === -0) // true
Copy the code
In ES6,Object added an IS method to fix these two bugs.
The object.is () function is the same as the === type. It also determines whether two variables are equal, but it fixes the two bugs that NaN is not equal to itself and that +0 is equal to -0.
Object.is(), === and == difference
- If two equal signs are judged equal, they will be cast during the comparison.
- Equal signs are equal (strict), no implicit type conversion is performed, (false is returned if different types are used)
- When using object. is for equality, it is generally the same as for triequals. It handles some special cases where -0 and +0 are no longer equal and two nans are considered equal.
conclusion
Thank you for seeing this