JS Operators


JavaScript operators are used to assign values, compare values, perform arithmetic operations, etc.


For the JavaScript Operators tutorial, check out JavaScript Operators Tutorial .


JavaScript arithmetic operators

Arithmetic operators are used to perform operations on two variables or values.

Assigning y = 5 , the following table will show you the use of arithmetic operators:

OperatorDescriptionExampley valuex valueTry It
+additionx = y + 2y = 5x = 7 Try It! 
-subtractionx = y - 2y = 5x = 3 Try It! 
*multiplicationx = y * 2y = 5x = 10 Try It! 
/divisionx = y / 2y = 5x = 2.5 Try It! 
%remainderx = y% 2y = 5x = 1 Try It! 
++auto incrementx = ++ yy = 6x = 6 Try It! 
x = y ++y = 6x = 5 Try It! 
--Decrementx = --yy = 4x = 4 Try It! 
x = y--y = 4x = 5 Try It! 

For arithmetic operators, you can read our JavaScript Operators tutorial .


JavaScript assignment operator

The assignment operator is used to assign values to JavaScript variables.

Given x=10 and y=5 , the following table explains the assignment operators:

OperatorExample1Example2x valueTry It
=x = yx = yx = 5 Try It! 
+=x + = yx = x + yx = 15 Try It! 
-=x - = yx = x - yx = 5 Try It! 
*=x * = yx = x * yx = 50 Try It! 
/=x / = yx = x / yx = 2 Try It! 
%=x% = yx = x% yx = 0 Try It! 

For assignment operators, you can read our JavaScript Operators tutorial .


JavaScript string operators

+ operator, += operator can be used to concatenate strings.

Given text1 = "Good " , text2 = "Morning" , and text3 = "" , the following table explains the use of string operators:

OperatorExampleText1Text2Text3Try It
+text3 = text1 + text2"Good ""Morning" "Good Morning" Try It! 
+=text1 += text2"Good Morning""Morning""" Try It! 

 


Comparison operator

Comparison operators are used in logical statements to determine whether two given values or variables are equal.

Given x=5 , the following table shows the use of comparison operators:

OperatorDescriptionCompareResultTry It
==equalx == 8false Try It! 
x == 5true Try It! 
===Both value and type are equal (constant equal)x === "5"false Try It! 
x === 5true Try It! 
!=not equal tox != 8true Try It! 
!==Both value and type are not equal (not equal)x !== "5"true Try It! 
x !== 5false Try It! 
>more than thex > 8false Try It! 
<less thanx < 8true Try It! 
>=greater than or equal tox >= 8false Try It! 
<=less than or equal tox <= 8true Try it! 

For comparison operators, you can read our JavaScript Comparison Operators tutorial .


Conditional operator

Conditional operators are used for conditional assignment operations.

Given x=6 and y=3 , the following table demonstrates the operation of the conditional operator:

GrammarExample Try It
variable = ( condition ) ? value1 : value2voteable = (age < 18) ? "too young": "age is ok"; Try It! 



Logical Operators

Logical operators are used to determine logical relationships between variables or values.

Given x=6 and y=3 , the following example demonstrates the use of logical operators:

operatordescribeexample
&&and(x < 10 && y > 1) is true
||or(x == 5 || y == 5) is false
!No!(x == y) is true



JavaScript bitwise operators

Bitwise operators work on 32-bit numbers. Any numeric operations will be converted to 32 bits. The result is converted to a JavaScript number.

OperatorDescriptionexamplesimilar toresultdecimal
&ANDx = 5 & 10101 & 00010001 1
|ORx = 5 | 10101 | 00010101 5
~Take the oppositex = ~ 5 ~01011010 -6
^XORx = 5 ^ 10101 ^ 00010100 4
<<Shift leftx = 5 << 10101 << 11010 10
>>Shift rightx = 5 >> 10101 >> 100102