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

Summary of 6 operators in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:33:441383browse

JavaScript operators mainly include:

  1. Arithmetic operators
  2. Assignment operator
  3. Comparison operators
  4. Ternary operator
  5. Logical operators
  6. String concatenation operator

Arithmetic operators

运算符 说明 例子 运算结果
y = 2 1 y = 3
- y = 2-1 y = 1
* y = 2*3 y = 6
/ 除,返回结果为浮点类型 y = 6/3 y = 2
% 求余,返回结果为浮点类型
要求两个操作数均为整数
y = 6%4 y = 2
递加,分为前加和后加
对布尔值和 NULL 将无效
y = 2
y(前加)
y (后加)
y = 3
-- 递减,分为前递减和后递减
对布尔值和 NULL 将无效
y = 2
--y(前减)
y--(后减)
y = 1

For pre-add and post-add, the result after execution is the variable plus 1. The difference is that the return result is different during execution. Please refer to the following two examples:

Copy code The code is as follows:

var x = 2;
alert(x); //Output: 3
alert(x); //Output: 3

var y = 2;
alert(y); //Output: 2
alert(y); //Output: 3

The same goes for decreasing.

Assignment operator

Assignment operator = is used for assignment operations. 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:

运算符 例子 等价于 运算结果
= y = 6 y = 6
= y = 1 y = y 1 y = 7
-= y -= 1 y = y-1 y = 5
*= y *= 2 y = y*2 y = 12
/= y /= 2 y = y/2 y = 3
%= y %= 4 y = y%4 y = 2

Use nested assignment operations

Assignment operators can be nested:

Copy code The code is as follows:

y = (x = 2) 5; //Result: x=2, y=7

Comparison operators

Operator Description Example Operation result
== Equal 2 == 3 FALSE
=== Identity (both values ​​and types need to be compared) 2 === 2
运算符 说明 例子 运算结果
== 等于 2 == 3 FALSE
=== 恒等于(值和类型都要做比较) 2 === 2
2 === "2"
TRUE
FALSE
!= 不等于,也可写作<> 2 == 3 TRUE
> 大于 2 > 3 FALSE
< 小于 2 < 3 TRUE
>= 大于等于 2 >= 3 FALSE
<= 小于等于 2 <= 3 TRUE
2 === "2"
TRUE FALSE
!= is not equal to, it can also be written as <> 2 == 3 TRUE
> Greater than 2 > 3 FALSE
< Less than 2 < 3 TRUE
>= Greater than or equal 2 >= 3 FALSE
<= Less than or equal 2 <= 3 TRUE

Comparison operators can also be used for string comparisons.

Ternary operator

Ternary can be regarded as a special comparison operator:

Copy code The code is as follows:

(expr1) ? (expr2) : (expr3)

Syntax explanation: When expr1 evaluates to TRUE, the value of the entire expression is expr2, otherwise it is expr3.

Example:

Copy code The code is as follows:

x = 2;
y = (x == 2) ? x : 1;
alert(y); //Output: 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.

Tips

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

Logical operators

String concatenation operator

The connection operator is mainly used to connect two strings or string variables. Therefore, when you use this operator on strings or string variables, you are not adding them. Example:
Copy code

The code is as follows:



x = "beijing";

y = x "Hello!"; //Result: y = "Hello beijing!"

// To add spaces between two strings, you need to insert spaces into a string:

y = x "Hello!"; //Result: y = "Hello beijing!"

Copy code


The code is as follows:
x = 25; y = "I am this year" x "years old"; //Result: y = "I am 25 years old this year"
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
Operator Description Example Operation result
&& Logical AND x = 2;
运算符 说明 例子 运算结果
&& 逻辑与(and) x = 2;
y = 6;
x && y > 5
FALSE
|| 逻辑或(or) x = 2;
y = 6;
x && y > 5
TRUE
! 逻辑非,取逻辑的反面 x = 2;
y = 6;
!(x > y)
TRUE
y = 6;

x && y > 5

FALSE
|| Logical OR x = 2; y = 6; x && y > 5 TRUE
! Logical negation, take the opposite side of logic x = 2; y = 6; !(x > y) TRUE