JavaScript operators

JavaScript operators mainly include:

Arithmetic operators

Assignment operators

Comparison operators

Ternary operator

Logical operator

String concatenation operator


Arithmetic operators


##Operators          

Description                                                         Operator                        ##+ Add y = 2+1 y = 3

- Subtract y = 2-1 y = 1

* Multiply y = 2*3 y = 6

/ Division, the return result is a floating point type y = 6/3 y = 2

% Find remainder, the return result is a floating point type Requires two operations The numbers are all integers y = 6%4 y = 2

++ Increasingly, divided into pre-add and post-add. It will be invalid for Boolean values ​​and NULL y = 2 ++y (pre-add) y++ (post-add) Add) y=3

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

var x = 2;

alert(++x); //Output: 3

alert(x); //Output: 3

var y = 2;

alert(y++); / /Output: 2

alert(y); //Output: 3







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

##Operator

Example is equivalent to

Operation result


##= y = 6 empty 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


##Comparison operations Symbol

Operator Explanation Example Operation result

== Equal to 2 == 3 FALSE

=== Constantly equal (both values ​​and types must be compared) ( 2 === 2 TRUE ) (

2 === "2" FALSE )

!= Not equal, it can also be written as <> 2 == 3 TRUE

> 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


ternary operator

Ternary operator can be regarded as a special comparison Operator:

(expr1) ? (expr2) : (expr3)


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

Example:

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.


Logical Operators

Operator Explanation Example Operation result

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

|| Logical OR (or) x = 2; y = 6; The opposite of x = 2; y = 6; !(x > y) TRUE


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

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 = "beijing you OK! "


When performing the concatenation (addition) operation on strings and numbers, the numbers will be converted into strings and then concatenated (added):

x = 25;

y = "I am this year" + x + "years old"; //Result: y = "I am 25 years old this year"


<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
txt1="What a very";
txt2="nice day";
txt3=txt1+txt2;
document.getElementById("demo").innerHTML=txt3;
}
</script>
</body>
</html>

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <Script> var x = 11; var y = 5; with (document) { write("x = 11, y = 5"); write("<LI>x + y 是 ", x + y); write("<LI>x - y 是 ", x - y); write("<LI>x * y 是 ", x * y); write("<LI>x / y 是 ", x / y); write("<LI>x % y 是 ", x % y); write("<LI>++ x 是 ", ++ x); write("<LI>-- y 是 ", -- y); } </Script> </head> <body> </body> </html>
submitReset Code