1. Multiplication operator
1. Multiplication: *
Some special rules for multiplication operators:
If the operands are all numerical values, according to conventional multiplication, if the product exceeds the representation range of the ECMAscript value, return infinity or -infinity
If an operand is NaN, the return result is NaN
If infinity is multiplied by 0, return NaN
If infinity is multiplied by a non-0 number, return infinity or -infinity
Multiply infinity with infinity and return infinity
If there is an operand that is not a numeric value, the background will first call number() to convert it into a numeric value, and then apply the above rules
The code copy is as follows:
<script type="text/javascript">
alert(5 * 6); //30
alert(5 * NaN); //NaN
alert(Infinity * 0); //NaN
alert(Infinity * 2); //Infinity
alert("5" * 5); //25
alert(true * 10); //10
alert(false * 10); //0
</script>
2. Division:/
Some special rules for division operators:
If the operands are all numerical values, according to conventional division calculation, if the quotient exceeds the representation range of the ECMAscript value, return infinity or -infinity
If an operand is NaN, the return result is NaN
If infinity is expelled by infinity, return to NaN
If 0 is divided by 0, return NaN
If the finite number of non-0 is divided by 0, return infinity or -infinity
If infinity is divided by a finite number that is not zero, return infinity or -infinity
If there is an operand that is not a numeric value, the background will first call number() to convert it into a numeric value, and then apply the above rules
The code copy is as follows:
<script type="text/javascript">
alert(5 / 5); //1
alert(5 / NaN); //NaN
alert(Infinity / Infinity); //NaN
alert(Infinity / 2); //Infinity
alert(5 / 0); //Infinity
alert(10 / true); //10
alert(10 / false); //Infinity
</script>
3. Find the module (remaining): %
Some special rules for finding modular operators:
If the operands are all numerical values, calculate according to conventional division method and return the remaining number obtained by dividing
If the dividend is infinite and the dividend is a finite number, the return result is NaN
If the dividend is finite and the dividend is 0, return NaN
If infinity is expelled by infinity, return to NaN
If the dividend is finite and the dividend is infinite, return the dividend
If the dividend is 0, return 0
If there is an operand that is not a numeric value, the background will first call number() to convert it into a numeric value, and then apply the above rules
The code copy is as follows:
<script type="text/javascript">
alert(26 % 5); //1
alert(Infinity % 3); //NaN
alert(3 % 0); //NaN
alert(5 % Infinity); //5
alert(0 % 10); //0
alert(true % 25); //1
alert(3 % false); //NaN
</script>
2. Additive operators
1. Addition operator: +
If one of the operands is a string:
If both operands are strings, then splice the second operand after the first operand.
If only one operand is a string, then convert another operand into a string and then execute the above rules
The code copy is as follows:
<script type="text/javascript">
var result1 = 5 + 5; // Add numbers to numbers
alert(result1); //10
var result2 = 5 + "5"; //Add a string with a number
alert(result2); //"55"
</script>
2. Subtraction operator:-
If an operand is a string, boolean, null, or undefined, then call number() in the background to convert it into a numeric value and then perform subtraction.
3. Relational operators
Greater than:>
Less than: <
Greater than or equal to:>=
Less than or equal to: <=
Relational operator special rules:
If the operand is a string, compare the corresponding character encoding of the two strings
If one operand is a numeric value, then convert the other operand into a numeric value and then compare
Any number is compared with NaN and the result is false
IV. Equal operator
1. Equal and unequal: == and! =
Both operators will convert the operand to the same type before comparing
When converting, the equality and unequal operators follow the following rules:
If one of the operands has type Boolean, then first convert it to a numeric type, false to 0, true to convert it to 1.
If one of the operands has a type of string and the other is a numeric type, then convert the string to a number for comparison.
If one of the operands is an object and the other is not, first call the valueof() method of the operand, get the basic type value and then compare it
Special rules for comparison:
null and undefined are equal.
null and undefined will not be converted to any other type
If the result of any operation is NaN, then equal comparison returns false, and unequal comparison returns true. Note that even if both operands are NaN, the return result is false, that is, NaN does not equal NaN.
If both operands are objects, then compare the values they refer to, if the same object is referenced, then return true, otherwise return false.
2. Congruence and Incompleteness: == and ===
==The operand will be converted to the same type for comparison;
===The type will not be converted, directly compare
For example:
The code copy is as follows:
var result1 = ("55" == 55);
var result2 = ("55" === 55);
alert(result1);//true
alert(result2); //false "55" is a string, 55 is a number, and the types are different
V. Conditional Operator
Variable = conditional expression? True value: false value
First, the conditional expression will be evaluated. If the result is true, the true value will be assigned to the variable. If false, the false value will be assigned to the variable.
The code copy is as follows:
<script type="text/javascript">
var num1 = 10;
var num2 = 25;
var num3 = (num2 > num1) ? num2 : num1;
alert(num3); //25
</script>
6. Assignment operator
1. Simple assignment operator: =
var num1 = 10;
num=num1+10;
2. Compound assignment operators: +=, -=, *=, /=, %=, >>=, <<=, >>>=
The code copy is as follows:
<script type="text/javascript">
var num = 5;
alert(num); //5
num += 5;
alert(num); //10
num *= 2;
alert(num); //20
num /= 10;
alert(num); //2
num -= 2;
alert(num); //0
</script>
7. Comma operator
The comma operator can perform multiple operations in a single statement
Purpose: 1. Declare multiple variables
var num1=1, num2=2, num3=3;
2. Assignment
var num=(0,1,2,3)//num=3
When used for assignment operations, the comma operator always returns the value of the last expression.
Practice area:
The code copy is as follows:
<script type="text/javascript">
var num1 = 5;
var num2 = 10;
var message = "The sum of 5 and 10 is " + num1 + num2;
alert(message);
</script>
The code copy is as follows:
<script type="text/javascript">
alert(null == undefined);
alert(null === undefined);
alert("NaN" == NaN);
alert("NaN" === NaN);
alert(NaN == NaN);
alert(NaN === NaN);
alert(NaN != NaN);
alert(NaN !== NaN);
alert(false == 0);
alert(false === 0);
alert(true == 1);
alert(true === 1);
alert(null == 0);
alert(undefined == 0);
alert(5 == "5");
alert(5 === "5");
</script>
The above is all about this article, and the explanation of JavaScript operators ends here. In the next article, we will explain JavaScript statements.