JavaScript operators

JavaScript operators mainly include:

Arithmetic operators

Assignment operators

Comparison operators

Ternary operator

Logical operator

String concatenation operator


Arithmetic operators


##Operators

DescriptionOperator##+ 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

Exampleis 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

OperatorExplanationExampleOperation 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

OperatorExplanationExampleOperation 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"


    php中文网(php.cn) 


Continuing Learning
||
php中文网(php.cn)
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!