0% found this document useful (0 votes)
13 views

Lesson 5 - JavaScript Operators

This document discusses JavaScript operators. It defines what operators are and lists the main types: arithmetic, comparison, logical, assignment, and conditional. It provides examples of each operator type and how they are used in JavaScript. Code examples are provided to demonstrate several operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lesson 5 - JavaScript Operators

This document discusses JavaScript operators. It defines what operators are and lists the main types: arithmetic, comparison, logical, assignment, and conditional. It provides examples of each operator type and how they are used in JavaScript. Code examples are provided to demonstrate several operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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

Lets have a look on all operators one by one


Arithmetic Operators
JavaScript supports the following arithmetic operators −
Assume variable A holds 10 and variable B holds 20, then

+ (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.

< (Less than)


Checks if the value of the left operand is less than the value of the
right operand, if yes, then the condition becomes true.
Ex: (A < B) is true.
Comparison Operators
>= (Greater than or Equal to)
Checks if the value of the left operand is greater than or equal to the
value of the right operand, if yes, then the condition becomes true.
Ex: (A >= B) is not true.

<= (Less than or Equal to)


Checks if the value of the left operand is less than or equal to the
value of the right operand, if yes, then the condition becomes true.
Ex: (A <= B) is true.
CODE EXAMPLES
Logical Operators
JavaScript supports the following logical operators −
Assume variable A holds 10 and variable B holds 20, then

&& (Logical AND)


If both the operands are non-zero, then the condition
becomes true.
Ex: (A && B) is true.
Logical Operators
|| (Logical OR)
If any of the two operands are non-zero, then the condition
becomes true.
Ex: (A || B) is 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

+= (Add and Assignment)


It adds the right operand to the left operand and assigns the
result to the left operand.
Ex: C += A is equivalent to C = C + A
Assignment Operators
−= (Subtract and Assignment)
It subtracts the right operand from the left operand and assigns
the result to the left operand.
Ex: C -= A is equivalent to C = C – A

*= (Multiply and Assignment)


It multiplies the right operand with the left operand and assigns
the result to the left operand.
Ex: C *= A is equivalent to C = C * A
Assignment Operators
/= (Divide and Assignment)
It divides the left operand with the right operand and assigns the
result to the left operand.
Ex: C /= A is equivalent to C = C / A

%= (Modules and Assignment)


It takes modulus using two operands and assigns the result to
the left operand.
Ex: C %= A is equivalent to C = C % A
CODE EXAMPLES
Miscellaneous Operator
We will discuss two operators here that are
quite useful in JavaScript: the conditional
operator (? :) and the typeof operator.
Conditional Operator (? :)
The conditional operator first evaluates an
expression for a true or false value and then
executes one of the two given statements
depending upon the result of the evaluation.

? : (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.

The typeof operator evaluates to "number", "string", or


"boolean" if its operand is a number, string, or boolean
value and returns true or false based on the evaluation.
typeof Operator
Here is a list of the return values for the typeof Operator.
CODE EXAMPLES
QUESTIONS?
Next Lesson:
Java Script
Control Structures

You might also like