Expressions and operators play very important roles in any programming language. To understand this, let's take a simple expression of
5+7=12
Here in this expression, 5
and 7
are referred to as operands and +
is known as the operator while the whole expression is referred to as the operation of addition. JavaScript provides a number of different operators:
These are the arithmetic operators that are supported in JavaScript:
+
for example 2 + 3 = 5
–
for example 10 - 20
will equals -10
*
for example 10*20
will equals 200
/
for example 20/10
gives 2
%
which provides the remainder of an integer division for example 20 % 10
will be 0
. ++
which increases the value of integer by one. For example, 10++
will give 11
--
which decreases the value of integer by one. For example, 10--
will give 9
. Comparison operators provide a means to compare two values. The following comparison operators are supported.
==
for comparing two values and giving true result if they are equal to each other. For Example, 10 == 20
is not true (false). !=
for comparing two values and giving true result if they are not equal to each other. For Example, 10 != 20
is true. >
for comparing two values and returning true if the left side is greater than the right side. For Example, 20 > 10
is true. <
for comparing two values and returning true if the left side is less than the right side. For Example, 10 < 20
is true. >=
for comparing two values and returning true if the left operand is greater than or equal to the right operand. For Example, 20 >= 10
is true. 20 <= 10
is not true. For example if x=true
and y=true
then x
&& y
is true and if x=true
and y=false
then x && y
is false
For example if x=true and y=false then x || y is true and if x=false and y=false then x || y is false
For example if x=true
then !x
is false.
=
, assigns the value of the right side to the left side. For example, given y = 5
and z = 10,
then x = y+z
assigns the value 15
to x
+=
, performs the addition of the left operand to the right operand and then assigns the result to the left operand. For example, given x
= 5 and z = 11, then x += z assigns the value 16 to x
-=
, performs the subtraction of the right operand from the left operand and then assigns the result to the left operand. For example, given x = 11 and z = 5, then x -= z assigns the value 6 to x
*=
, performs the multiplication of the right operand with the left operand and then assigns the result to the left operand. For example, given x = 11 and z = 5, then x *= z assigns the value 55
to x
For example, given x = 10 and z = 5, then x /= z assigns the value 2
to x
%=
, performs modulus using two operands and assigns the result to the left operand. For example, X%= Z is equal to X=X%A. For example, given x = 11 and z = 5, then x %= z assigns the value 1
to x