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

Summary of 6 operators in JavaScript

巴扎黑
巴扎黑Original
2017-07-29 17:12:261382browse

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

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 = 6 y = 6
##/=
y = y/2 y = 3 %=
y = y%4 y = 2

Code As follows:

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

Comparison operator

OperatorExplanation is equal toEqual (values ​​and types must be compared) 2 === "2"TRUE is not equal, and can also be written as a8093152e673feb7aba1828c435320942 == 3TRUE> is greater than 2 > 3FALSE033f6d3348334946520499986320dc63=2 >= 3x = 2; y = 6;x = 2; y = 6;
Example Operation result ==
2 == 3 FALSE ===
2 === 2 FALSE
##!=
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!

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