This article mainly gives you a detailed summary of the operators in JavaScript, including common arithmetic operators, comparison operators and logical operators. It is very clear, friends who need it can refer to it.
In JavaScript, common operators include arithmetic operators, comparison operators and logical operators.
Table 1 JavaScript common operators
Arithmetic operators | Explanation | Examples | Result |
---|---|---|---|
= | Assignment operator. Assign the value of the variable on the right side of the operator to the variable on the left. | x = 5; | - |
+ | plus sign. Add two numbers. | y=1+2; | y=3 |
- | Minus sign. Subtract two numbers. | z = x-y; | z=2 |
* | Multiply sign. Multiply two data together. | a=x*y; | a=15 |
/ | Division sign. Divide two data. | b=x/z; | b=2.5 |
% | Remainder operation. Find the remainder after dividing two numbers. | c=x%z; | c=1 |
++ | Self-added. Add 1 to the operand. | m=++x; | m=6 x=6 |
decrement. Decrement the operand by 1. | n=--x; | n=5 x=5 | |
Description | Example | Result | |
Equal. If the two data are equal, return true, otherwise return false. | boolean1=(x==5); | boolean1=true | ##!= |
boolean2=(x!=5); | boolean2=false; | > | |
boolean4=(x>y); | boolean4=true | < | |
boolean5=(x | >= | ||
boolean6=(x>=y); | boolean6=true | <= | |
boolean7=(x<=y); | boolean7=false | Logical operator | |
Example | Result | && | |
boolean_a=true&&false; | boolean_a=false | || | |
boolean_b=true||false; | boolean_b=true | ! | |
boolean_c=!true; | boolean_c=false |
The "+" sign can not only add two data, but can also be used to connect strings.
For example: The code is as follows:var name=" Tom "; var age=22; var person="My name is "+name+" ! I'm "+age+" ! "; alert(person);
Discussion on self-increasing (++) and self-decreasing (--)
It is worth noting that self-increasing (++) and self-decreasing (- -) The operator has different meanings when placed before and after the operand. If it is placed in front of the operand (front self-increment/front self-decrement), the operand will be added by 1 (subtracted by 1) first, and then the operation will be performed; if it is placed after the operand (last self-increment/back self-decrement), the operation will be performed first. Then add 1 to the operand (decrease 1).
For example: The code is as follows:<script type="text/javascript"> var x=5; var y=++x; // 前自加,赋值后 x 的值为 6 var z=x++; // 后自加,赋值后 x 的值为 7 var m=--x // 前自减,赋值后 x 的值为 6 var n=x-- // 后自减,赋值后 x 的值为 5 </script> <p onclick="alert(y);">显示 y 的值</p> <p onclick="alert(z);">显示 z 的值</p> <p onclick="alert(m);">显示m 的值</p> <p onclick="alert(n);">显示 n 的值</p>
Analysis:
For z, first pass the value of x (x=6) to z, then add 1 to x, and the value becomes 7.
For m, the value of x (x=7) minus 1 is 6, and then the value of x is passed to m.For n, first pass the value of x (x=6) to n, then subtract 1 from x, and the value becomes 5.
Abbreviations of arithmetic operators
In order to facilitate operation and reduce code writing, JavaScript also supports abbreviations of common mathematical operators.
Table 2 Abbreviations of common arithmetic operatorsis equivalent to | += | |
---|---|---|
x=x+y | -= | |
x=x-y | *= | |
x=x*y | /= | |
x=x/y | %= | |
x=x%y |
The above is the detailed content of Summary of JavaScript operator usage. For more information, please follow other related articles on the PHP Chinese website!