This article mainly introduces the summary of 6 operators in JavaScript. This article summarizes arithmetic operators, assignment operators, comparison operators, ternary operators, logical operators, string concatenation operators, etc., which are required Friends can refer to
JavaScript operators mainly include:
Arithmetic operator
Explanation | Example | Operation result | |
---|---|---|---|
Add | y = 2+1 | #y = 3 | |
minus | y = 2-1 | y = 1 | |
times | y = 2*3 | #y = 6 | |
Division, the return result is a floating point type | y = 6/3 | y = 2 | |
Find the remainder, and the returned result is a floating point type | Both operands are required to be integers
y = 6%4 |
y = 2 | |
increment , divided into pre-adding and post-adding | Will have no effect on Boolean values and NULL
y = 2 |
++y (prefix)
y++ (post-addition) y = 3 |
|
Decreasing, divided into front decrease and back decrease | Will have no effect on Boolean values and NULL
y = 2 |
--y (front minus)
y--(post-subtraction) y = 1 |
The code is as follows:
var x = 2; alert(++x); //输出:3 alert(x); //输出:3 var y = 2; alert(y++); //输出:2 alert(y); //输出:3
The same goes for decrement.
Assignment operatorAssignment operator = is used for assignment operation. The assignment operator is used to assign the value on the right to the variable on the left. Set y = 6, see the table below:
Example | is equivalent to the | operation Result | |
---|---|---|---|
y = 6 | � | y = 6 | # #+= |
y = y+1 | y = 7 | -= | |
y = y-1 | y = 5 | *= | |
y = y*2 | y = 12 | ##/= | |
y = y/2 | y = 3 | %= | |
y = y%4 | y = 2 | Assignment operators can be nested using |
y = (x = 2) + 5; //结果: x=2,y=7
Operator
Example | Operation result | == | |
---|---|---|---|
2 == 3 | FALSE | === | |
2 === 2 | 2 === "2"TRUE |
FALSE | ##!= | is not equal, and can also be written as <>
TRUE | > | is greater than | |
FALSE | < | Less than | |
TRUE | ##>= | Greater than or equal to | |
FALSE | <= | Less than or equal to | |
TRUE | Comparison operators can also be used for string comparisons. |
Ternary can be regarded as a special comparison operator:
The code is as follows:(expr1) ? (expr2) : (expr3)
Example:
The code is as follows:
x = 2; y = (x == 2) ? x : 1; alert(y); //输出:2
This example determines whether the value of x is equal to 2. If x is equal to 2, then the value of y is equal to x (that is, equal to 2), otherwise y is equal to 1.
TipTo avoid errors, it is a good idea to enclose each expression of the ternary operator in parentheses.Operator
ExplanationOperation result | && | Logical AND (and) | |
---|---|---|---|
x && y > 5 | FALSE | || | Logical OR |
x && y > 5 | TRUE | ! | Logical negation, take the opposite side of logic |
!(x > y) | TRUE | 字符串连接运算符 连接运算符 + 主要用于连接两个字符串或字符串变量。因此,在对字符串或字符串变量使用该运算符时,并不是对它们做加法计算。 例子: 代码如下: x = "beijing"; y = x + "你好!"; //结果:y = "beijing你好!" // 要想在两个字符串之间增加空格,需要把空格插入一个字符串之中: y = x + " 你好!"; //结果:y = "beijing 你好!" Copy after login 当对字符串和数字做连接(加法)运算时,会将数字先转换成字符串再连接(相加): 代码如下: x = 25; y = "我今年" + x + "岁"; //结果:y = "我今年25岁" Copy after login The above is the detailed content of Summary of 6 operators in JavaScript. For more information, please follow other related articles on the PHP Chinese website!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|