Home > Web Front-end > JS Tutorial > Summary of 6 operators in JavaScript

Summary of 6 operators in JavaScript

巴扎黑
Release: 2017-07-29 17:12:26
Original
1402 people have browsed it

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:

  1. ##arithmetic operators

  2. Assignment operator

  3. Comparison operator

  4. Ternary operator

  5. Logical operator

  6. String concatenation operator

Arithmetic operator

OperatorExplanationExampleOperation result##+-*/% Both operands are required to be integers++ Will have no effect on Boolean values ​​and NULL ++y (prefix)-- Will have no effect on Boolean values ​​and NULL --y (front minus)For pre-addition and post-addition, the result after execution is the variable plus 1. The difference The return results are different during execution. Please refer to the following two examples:
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
y = 6%4
y = 2
increment , divided into pre-adding and post-adding
y = 2
y++ (post-addition)

y = 3
Decreasing, divided into front decrease and back decrease
y = 2
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
Copy after login

The same goes for decrement.

Assignment operator

Assignment 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:

Operator=# #+=y += 1y = y+1y = 7-=y -= 1y = y-1y = 5*=y *= 2y = y*2y = 12y /= 2y %= 4Assignment operators can be nested using Assignment operators can be nested:
Example is equivalent to the operation Result
y = 6y = 6
##/=
y = y/2y = 3%=
y = y%4 y = 2

Code As follows:

y = (x = 2) + 5;    //结果: x=2,y=7
Copy after login

Comparison operator

OperatorExplanation is equal toEqual (values ​​and types must be compared) 2 === "2"TRUE is not equal, and can also be written as <>2 == 3TRUE> is greater than 2 > 3FALSE< Less than2 < 3TRUE2 >= 32 <= 3Comparison operators can also be used for string comparisons. Ternary operator
ExampleOperation result==
2 == 3FALSE===
2 === 2 FALSE
##!=
##>=Greater than or equal to
FALSE<=Less than or equal to
TRUE

Ternary can be regarded as a special comparison operator:

The code is as follows:

(expr1) ? (expr2) : (expr3)
Copy after login
Syntax explanation: When expr1 evaluates to TRUE, the value of the entire expression is expr2, otherwise it is expr3.

Example:

The code is as follows:

x = 2;
y = (x == 2) ? x : 1;
alert(y); //输出:2
Copy after login

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.

Tip

To avoid errors, it is a good idea to enclose each expression of the ternary operator in parentheses.


Logical Operator

Operator

ExplanationExamplex = 2; y = 6;x = 2; y = 6;x = 2; y = 6;
Operation result&&Logical AND (and)
x && y > 5FALSE

||
Logical OR
x && y > 5TRUE

!
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template