JavaScript operators


JavaScript Operators


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


JavaScript Arithmetic Operators

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

Assignmenty = 5, The following table will explain to you the use of arithmetic operators:

OperatorDescriptionExampley valuex valueOnline example
+ Additionx = y + 2y = 5x = 7Example»
-Subtractionx = y - 2y = 5x = 3Example»
*Multiplicationx = y * 2y = 5x = 10 Example»
/Divisionx = y / 2y = 5x = 2.5Example»
%remainderx = y % 2y = 5x = 1Instance»
++incrementx = + +yy = 6x = 6Instance»
x = y++y = 6x = 5Example»
--decrementx = - -yy = 4x = 4Instance»
x = y--y = 4x = 5Example »

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:

##-=x -= y#x = x - yx = 5Example»* =x *= yx = x * yx = 50Example»/=x /= yx = x / yx = 2Example»%=x %= yx = x % yx = 0Example»

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:

OperatorExampleSame Asx ValueOnline Example
=x = yx = yx = 5Example»
+= x += y#x = x + yx = 15Example»
OperatorExampletext1text2text3Online example
+text3 = text1 + text2"Good ""Morning" "Good Morning"Example»
+ =text1 += text2"Good Morning""Morning"""Example»


Comparison operators

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

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

## !==The value and type are not equal (not equal)x !== "5"trueInstance»x !== 5falseInstance»> Greater than x > 8falseExample»<Less than x < 8trueInstance»>= is greater than or equal to x >= 8falseInstance»<= is less than or equal to x <= 8Example»

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


Conditional operator

The conditional operator is used for assignment operations based on conditions.

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

OperatorDescriptionComparisonResultOnline instance
==equalsx == 8falseInstances»
x == 5trueInstances »
===The value and type are equal (identical to)x === "5" falseInstance»
x === 5trueInstance»
!= is not equal to x != 8trueExample»
true
SyntaxExampleOnline example
Variable= (Condition) ? Value 1:Value 2voteable = (age & 18) ? "Too young" : "Old enough"Example»


Logical operators

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

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

##&&and(x < 10 && y > 1) is true##||!
OperatorDescriptionExample
or (x == 5 || y == 5) is false
Not!(x == y) is true
##JavaScript Bit operators

Bit 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 1OR##~ Negationx = ~ 5 ~01011010 10x = 5 ^ 1##<<Shift left/td>x = 5 << 10101 << 11010 10>>Shift rightx = 5 >> 10101 >> 100102
##|
x = 5 | 10101 | 0001 0101 5
##^XOR
0101 ^ 00010100 4