Summary of six operators in JavaScript

  • 2020-03-30 04:06:39
  • OfStack

JavaScript operators mainly include:

(link: #) (link: #) (link: #) (link: #) (link: #) (link: #)

The operator instructions example Operation results
+ add y = 2+1 y = 3
- Reduction of y = 2-1 y = 1
* take y = 2*3 y = 6
/ Divide, and return the result as a floating point type y = 6/3 y = 2
% Remainder, returns the result as a floating point type
Both operands are required to be integers
y = 6%4 y = 2
++ Step by step, divided into before and after
The Boolean sum NULL Will be invalid
y = 2
++y (before)
y++ (after)
y = 3
-- Decline, divided into former decline and after the decline
The Boolean sum NULL Will be invalid
y = 2
--y (cut)
y-- After (minus)
y = 1

For the former plus and the latter, the result after execution is variable plus 1, the difference is that the result returned during execution is different, refer to the following two examples:


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

The same thing as decreasing.

The assignment operator = is used for the 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 following table:

The operator example Is equivalent to Operation results
= 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

Assignment operations are nested

Assignment operators can be nested using:


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

The operator instructions example Operation results
== Is equal to the 2 == 3 FALSE
=== Identical to (value and type are compared) 2 === 2
2 === "2"
TRUE
FALSE
!= Not equal to, also can write <> 2 == 3 TRUE
> Is greater than 2 > 3 FALSE
< Less than 2 < 3 TRUE
>= Greater than or equal to 2 >= 3 FALSE
<= Less than or equal to 2 <= 3 TRUE

The comparison operator can also be used for string comparisons.

Ternary can be thought of as a special comparison operator:


(expr1) ? (expr2) : (expr3)

Syntax: the value of the entire expression is expr2 when expr1 is TRUE, otherwise expr3.

Example:


x = 2;
y = (x == 2) ? x : 1;
alert(y); //Output: 2

If x is equal to 2, then y is equal to x, and y is equal to 1.

prompt

To avoid errors, it is a good idea to bracket the expressions of the ternary operators.

The operator instructions example Operation results
&& Logic and ( and ) x = 2;
y = 6;
x && y > 5
FALSE
|| Logic or ( or ) x = 2;
y = 6;
x && y > 5
TRUE
! Logic not, take the opposite of logic x = 2;
y = 6;
!(x > y)
TRUE

The join operator + is primarily used to join two strings or string variables. Therefore, when you use this operator on strings or string variables, you do not add them.

Example:


x = "beijing";
y = x + " Hello! "; //Result: y = "hello Beijing!" //To add a space between two strings, insert the space into a string:
y = x + " Hello! "; //Result: y = "hello Beijing!" < br / >

When concatenating a string and a number (adding), the number is converted to a string and concatenated (adding) :


x = 25;
y = " I this year " + x + " At the age of "; //Result: y = "I am 25 years old.


Related articles: