First, let’s do some questions first! In order to unify, I don’t mix these questions. In interview questions, I often mix these questions, which will make you more confused. In order to make it more convenient for demonstration, I have written some questions in modules here, so you can read it!
Implicit conversion of operator strings
multiplication
console.dir("--------------------------"); console.dir(5*"5"); console.dir(5*"a"); console.dir(5*NaN); console.dir(5*null); console.dir(5*undefined); console.dir(5*5); console.dir("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------division
console.dir("----------------------"); console.dir(5/"5"); console.dir(5/"a"); console.dir(5/NaN); console.dir(5/null); console.dir(null/5); console.dir(5/undefined); console.dir(5/5); console.dir(5/0); console.dir(0/5); console.dir(0/0); console.dir("------------------------"); console.dir("-----------------------------");Take the remaining and find the model
console.dir("---------------------------"); console.dir(16%"5"); console.dir(5%"a"); console.dir(5%NaN); console.dir(5%null); console.dir(null%5); console.dir(5%undefined); console.dir(5%5); console.dir(5%0); console.dir(0%5); console.dir(0%0); console.dir("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------addition
console.dir("-----------------------"); console.dir(16+"5"); console.dir(5+"a"); console.dir(5+NaN); console.dir(5+null); console.dir(5+undefined); console.dir(5+5); console.dir("The sum of two numbers is "+5+5); console.dir("The sum of two numbers is "+(5+5)); console.dir("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Subtraction
console.dir("----------------------"); console.dir(16-"5"); console.dir(5-"a"); console.dir(5-NaN); console.dir(5-null); console.dir(5-undefined); console.dir(5-5); console.dir(5-true); console.dir(5-"true); console.dir(5-""); console.dir("The difference between two numbers is "+5-5); console.dir("The difference between two numbers is "+(5-5)); console.dir("------------------"); console.dir("---------------------"); console.dir("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Relational operators
console.dir("--------------------"); console.dir(16>"5"); console.dir("16">"5"); console.dir(5<"a"); console.dir(5>=NaN); console.dir(5<NaN); console.dir(NaN>=NaN); console.dir(5>=null); console.dir(5>=undefined); console.dir(5>=5); console.dir(5>=true); console.dir(5>="true"); console.dir(5>=""); console.dir("Brick">"alphabet"); console.dir("brick">"alphabet"); console.dir("---------------------");multiplication
console.dir(5*"5"); //25 console.dir(5*"a");//NaN console.dir(5*NaN);//NaN console.dir(5*null);0 console.dir(5*undefined);//NaN console.dir(5*5);//25
Let’s talk about the principle of implicit conversion of multiplication:
1. If the two values are all numbers, then perform multiplication operations directly. (I believe everyone can do it, just like primary school mathematics, and you should pay attention to the symbols of numbers). If the product value exceeds the numerical representation range of ECMAscript, then return Infinity (positive infinity) or -Infinity (negative infinity)
2. If a number is NaN, then the result is NaN
3. If Infinity is multiplied with 0, the result is NaN
4. If one operator is a number and the other is not a numeric value, then first use the Number() function to convert it, and multiply the converted value with the number. If the converted result appears NaN, then the result is NaN.
division
console.dir(5/"5");//1 Convert characters into numbers and divide console.dir(5/"a");//NaN Console.dir(5/NaN);//NaN console.dir(5/null);//Infinity null converts with Number() function, and the result is 0, then 5/0 is the infinite console.dir(null/5);//0 The same as above 0/5 is 0 console.dir(5/undefined);//NaN undefined converts with Number(), and the result is NaN console.dir(5/5);//1 console.dir(5/0);//Infinity console.dir(0/5);//0 console.dir(0/0);//NaN //0 divided by 0 is NaN
Let’s talk about the implicit conversion principle of division:
Similar to multiplication, the only one more is that 0/0 result is NaN
Take the remaining and find the model
The most common thing to use in the project is to find odd and even numbers. We often use a number with 2 to calculate the balance. If the result is 0, then the number is an even number, and if the result is 1, then the number is an odd number.
Check out the above topic:
console.dir(16%"5"); //1 Convert the string 5 to 5 through Number() and then calculate the remaining console.dir(5%"a"); //NaN console.dir(5%NaN); //NaN console.dir(5%null); //NaN convert null through Number(), the result is 0, and then calculate 5%0, the result is NaN console.dir(null%5); //0 The same as above 0%5, the result is 0 console.dir(5%undefined); //NaN console.dir(5%5); //0 console.dir(5%0); //NaN console.dir(0%5); //0 console.dir(0%0);//NaNconsole.dir(Infinity%Infinity);//NaNconsole.dir(5%Infinity);//5 console.dir(Infinity%5); //NaN
Let’s talk about the principle of implicit conversion of residuals:
Like multiplication, let me talk about something special! We all know the concepts of dividends and divisors, which we learned in elementary school.
1. The dividend is infinite and the dividend is a finite value, so the result is NaN
2. The divisor is a finite value, and the divisor is 0, then the result is NaN
3. Infinity%Infinity result is NaN
4. The dividend is a finite value, the dividend is an infinite value, and the result is a divisor.
5. Divided number is 0, the result is 0
Subtraction
Take a look at the example above!
console.dir(16-"5");//11 console.dir(5-"a");//NaN console.dir(5-NaN);//NaN console.dir(5-null);//5 console.dir(5-undefined);//NaN console.dir(5-5);//0 console.dir(5-true);//4 console.dir(5-"true");//NaN console.dir(5-"");//5 console.dir(5-Infinity);//-Infinity console.dir(Infinity-Infinity);//NaN console.dir("The difference between two numbers is "+5-5);//NaN console.dir("The difference between two numbers is "+(5-5));//The difference between two numbers is 0Let’s talk about the principle of implicit conversion of subtraction:
Just like the above, I won’t talk about the same thing, I will talk about the unique subtraction.
1. Infinity-Infinity result is NaN
2. -Infinity-Infinity result is -Infinity
3. The result of a number minus Infinity is - Infinity
4. Infinity-(-Infinity) The result is Infinity
5. If the operand is an object, the object valueOf method is called. If the result is NaN, the result is NaN. If there is no valueOf method, then call toString() method and convert the resulting string to a numeric value.
Relational operators
Relational operators uniformly return true or false
console.dir(16>"5"); //true console.dir("16">"5");//false console.dir(5<"a");//false console.dir(5>=NaN);//false console.dir(5<NaN);//false console.dir(NaN>=NaN);//false console.dir(5>=null);//true console.dir(5>=undefined);//false console.dir(5>=undefined);//false console.dir(5>=5);//true console.dir(5>=true);//true console.dir(5>=undefined);//false console.dir(5>=5);//true console.dir(5>=true);//true console.dir(5>=true);//false console.dir(5>="");//true console.dir("Brick">"alphabet");//false The string encoding value of B is 66, while the string encoding of a is 97. Therefore, false console.dir("brick">"alphabet");//true The lowercase letter b is larger than a, so it is trueLet’s talk about the implicit conversion principle of relational operators:
It's still the same as above, I won't say the same thing.
If the two numbers compared are strings, the string encoded values corresponding to the string will be compared.
Addition operation
The reason why I finally said that the implicit conversion of addition operations is that the implicit conversion of addition operations is different from the previous ones. All previous operator symbols, as long as one is a number, the other also uses Number() to convert numbers by default. The addition operation is different. As long as one of the addition operations is a string, the other one will also be converted into a string and then string splicing is performed!
console.dir(16+"5"); //156 console.dir(5+"a");//5a console.dir(5+NaN);//NaN console.dir(5+null);//5 console.dir('5'+null);//5null console.dir(5+undefined);//NaN console.dir(null+undefined);//NaN console.dir(5+5);//10 console.dir("sum of two numbers is "+5+5);//Sum of two numbers is 55 console.dir("sum of two numbers is "+(5+5));//Sum of two numbers is 10Let’s talk about the implicit conversion principle of addition operators:
1. One is a string, and the other one will also be converted into a string for splicing. If one is a string and the other is null or undefined, then adding, null or undefined will call the String() method to obtain the string "null" or "undefined", and then splice it.
2. If a number is added with null or undefined, then null or undefined is converted into Number() and then added.
3. The remaining principles are similar to others, so I won’t say much.
Biequal implicit conversion
Run the following code once, and I believe you will understand it naturally~
var a;console.dir(0 == false);//trueconsole.dir(1 == true);//trueconsole.dir(2 == {valueOf: function(){return 2}});//trueconsole.dir(a == NaN);//falseconsole.dir(NaN == NaN);//falseconsole.dir(8 == undefined);//falseconsole.dir(1 == undefined);//falseconsole.dir(2 == {toString: function(){return 2}});//trueconsole.dir(undefined == null);//trueconsole.dir(null == 1);//falseconsole.dir({ toString:function(){ return 1 } , valueOf:function(){ return [] }} == 1);//trueconsole.dir(1=="1");//trueconsole.dir(1==="1");//false