Lesson 5 - JavaScript Operators
Lesson 5 - JavaScript Operators
CSC/ITS – 101(PROGRAMMING 1)
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 Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
+ (Addition)
Adds two operands
Ex: A + B will give 30
Arithmetic Operators
- (Subtraction)
Subtracts the second operand from the first
Ex: A - B will give -10
* (Multiplication)
Multiply both operands
Ex: A * B will give 200
Arithmetic Operators
/ (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
Arithmetic Operators
++ (Increment)
Increases an integer value by one
Ex: A++ will give 11
-- (Decrement)
Decreases an integer value by one
Ex: A-- will give 9
Note − Addition operator (+) works for Numeric as well as Strings. e.g. "a"
+ 10 will give "a10".
CODE EXAMPLES
Comparison Operators
JavaScript supports the following comparison operators −
Assume variable A holds 10 and variable B holds 20, then −
== (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; while A==“10” is true;
Comparison Operators
=== (Is Exactly Equal)
Checks if the value of two operands have equal value and equal
type or not, if yes, then the condition becomes true.
Ex: (A === “10”) 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.
Comparison Operators
> (Greater than)
Checks if the value of the left operand is greater than the value of
the right operand, if yes, then the condition becomes true.
Ex: (A > B) is not true.
! (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.
CODE EXAMPLES
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
? : (Conditional )
If Condition is true? Then value X : Otherwise value Y
typeof Operator
The typeof operator is a unary operator that is placed
before its single operand, which can be of any type. Its
value is a string indicating the data type of the operand.