Module 5 - Conditionals and Loops
Module 5 - Conditionals and Loops
What is an Operator?
Let us take a simple expression: 4 + 5 is equal to 9. Here 4 and 5 are called operands and ‘+’
is called the operator. JavaScript supports the following types of operators.
Arithmetic Operators
Comparison(Relational) Operators
Logical Operators
Assignment Operators
Arithmetic Operators
Assume variable A holds 10 and variable B holds 20, then
+ (Addition)
Adds two operands
Ex: A + B will give 30
- (Subtraction)
Subtracts the second operand from the first
Ex: A - B will give -10
* (Multiplication)
Multiply both operands
Ex: A * B will give 200
/ (Division)
Divide the numerator by the denominator
Ex: B / A will give 2
% (Modulus)
Outputs the remainder of an integer division
Ex: B % A will give 0
++ (Increment)
Increases an integer value by one
Ex: A++ will give 11
-- (Decrement)
Decreases an integer value by one
Ex: A-- will give 9
= = (Equal)
Checks if the value of two operands are equal or not, if yes, then the condition becomes
true.
Ex: (A == B) is not true.
!= (Not Equal)
Checks if the value of two operands are equal or not, if the values are not equal, then the
condition becomes true.
Ex: (A != B) is true.
Logical Operators
JavaScript supports the following logical operators −
Assume variable A holds 10 and variable B holds 20, then −
! (Logical NOT)
Reverses the logical state of its operand. If a condition is true, then the Logical NOT
operator will make it false.
Ex: ! (A && B) is false.
Assignment Operators
JavaScript supports the following assignment operators −
= (Simple Assignment )
Assigns values from the right side operand to the left side operand
Ex: C = A + B will assign the value of A + B into C