Home > Article > Web Front-end > Summary of 6 operators in JavaScript
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=7Comparison operator
Operator
Example | Operation result | == | |||
---|---|---|---|---|---|
2 == 3 | FALSE | === | |||
2 === 2 | 2 === "2"TRUE |
FALSE |
##!= | is not equal, and can also be written as a8093152e673feb7aba1828c43532094||
TRUE | > | is greater than | |||
FALSE | 033f6d3348334946520499986320dc63= | Greater than or equal to | |||
FALSE | 6a36f4998d8549aff8d4fe45a89fc428 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 你好!" 当对字符串和数字做连接(加法)运算时,会将数字先转换成字符串再连接(相加): 代码如下: x = 25; y = "我今年" + x + "岁"; //结果:y = "我今年25岁" |
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!