JavaScript Tutorial – Operators & IF-Else Statement
JavaScript Tutorial – Operators & IF-Else Statement
Operators
The Arithmatic Operators:
The Arithmetic Operators are used with numeric operands and returns a numerical value. There are following arithmatic operators supported by JavaScript language:
Let assume variable a=5 and variable b=10.
The Unchained Tour offers the java training with placements, and if you’re from Chennai make sure to check them out for your Java requirements
Operator
Description
Example
+
Addition
a + b = 15
-
Subtraction
a - b = -5
*
Multiplication
a * b = 50
/
Division
b / a = 2
%
Modulus (division remainder)
b % a = 0
++
Increment
a++ = 6
--
Decrement
a-- = 4
The Assignment Operators:
There are following assignment operators supported by JavaScript language:
Let assume variable a=20 and variable b=10.
Operator
Example 1
Same As Example 1
Result
=
a=b
a = 10
+=
a+=b
a=a+b
a = 30
-=
a-=b
a=a-b
a = 10
*=
a*=b
a=a*b
a = 200
/=
a/=b
a=a/b
a = 2
%=
a%=b
a=a%b
a = 0
The Comparison Operators:
There are following comparison operators supported by JavaScript language:
Let assume variable a=2 and variable b=3.
Operator
Description
Example
Result
==
is equal to
a == b
is not true
!=
is not equal
a != b
is true
>
is greater than
a > b
is not true
<
is less than
a < b
is true
>=
is greater than or equal to
a >= b
is not true
<=
is less than or equal to
a <= b
is true
The Logical Operators:
There are following logical operators supported by JavaScript language:
Let assume variable a=5 and variable b=8.
Operator
Description
Example
Result
&&
and
(a && b)
is true
||
or
(a || b)
is true
!
not
!(a && b)
is false
IF-Else Statement
JavaScript IF-Else Statement
JavaScript If Statement
· If statement is used to make a decision according to a given condition. The if statement evaluates the expression, if the value of expression is true then the instruction is executed.
Syntax:
if (expression){ Execute statement if expression is true }
Example:
<script type="text/javascript"> <!-- var a = 10; if( a > 7 ){ document.write("IF condition is fulfilled"); } //--> </script>
JavaScript If - Else Statement
· If statement is used to make a decision according to a given condition. The if statement evaluates the expression, if the value of expression is false then the else block is executed.
Syntax:
if (expression){ Execute statement if expression is true } else { Execute statement else if expression is false }
Example:
<script type="text/javascript"> <!-- var a = 10; if( a > 7 ){ document.write("IF condition is true"); } else { document.write("IF condition is false"); } //--> </script>
JavaScript If - Else - If Statement
Syntax:
if (condtion 1 ){ Execute statement if condtion 1 is true } else if (condtion 2 ){ Execute statement if condtion 2 is true } else if (condtion 3 ){ Execute statement if condtion 3 is true } else { Execute statement if all conditions are false }
Example:
<script type="text/javascript"> <!-- var a = "london"; if( a == "boston" ){ document.write("Boston city"); } else if(a == "paris" ){ document.write("Paris city"); } else if(a == "berlin" ){ document.write("Berlin city"); } else { document.write("City names do not match"); } //--> </script>