JavaScript Operators With Example

Card image cap

JavaScript Operators

An operator that performs some operation on single or multiple operands and produces a result is called Operators.

An Operators are symbols or keyword that enable JavaScript to perform some sort of actions.

There are different Operatros as Follows..

i). Arithmetic Operators

An Arithmetic operators are an operator which is used to perform mathematical operations between numeric operands.

Operator Description
+ Adds two numeric operands.
- Subtract right operand from left operand
* Multiply two numeric operands.
/ Divide left operand by right operand.
% Modulus operator. Returns remainder of two operands.
++ Increment operator. Increase operand value by one.
-- Decrement operator. Decrease value by one.

Example:

Result:

	14
	6
	40
	2.5
	2 

ii). Comparison Operators

The following are the comparison operators supported by JavaScript.

JavaScript comparison operator also determine the two operands satisfied the given condition

Operators Description
== Compares the equality of two operands without considering type.
=== Compares equality of two operands with type.
!= Compares inequality of two operands.
> Checks whether left side value is greater than right side value. If yes then returns true otherwise false.
< Checks whether left operand is less than right operand. If yes then returns true otherwise false.
>= Checks whether left operand is greater than or equal to right operand. If yes then returns true otherwise false.
<= Checks whether left operand is less than or equal to right operand. If yes then returns true otherwise false.

Example:

Result:

(a == b) => false 
(a < b) => true 
(a > b) => false 
(a != b) => true 
(a >= b) => false 
a <= b) => true
Set the variables to different values and different operators and then try...

Logical Operators

The logical operators are typically used to combine conditional statements

Operator Name Example Result
&& And x && y True if both x and y are true
|| Or x || y True if either x or y is true
! Not !x True if x is not true

Example:

Result:


  2020 is a leap year. 

Assignment Operators

An assignment operators perfoms an operation of assigning values to left operand based on right operand. equal (=) operators is used to assign a values.

Operator Description Example Is The Same As
= Assign x = y x = y
+= Add and assign x += $ x = x + y
-= Subtract and assign x -= y x = x - y
*= Multiply and assign x *= y x = x * y
/= Divide and assign quotient x /= y x = x / y
%= Divide and assign modulus x %= y x = x % y

Example:

Result:

10
50
30
125
5
10 

Conditional Operator (or) Ternary Operator

Conditional operator first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation.

Syntax:


	if(operand1 conditional operator operand2)? statement1:statement2

Example:


	if(a==b)?1:0


Bitwise Operator:

JavaScript bitwise operators evaluate and perform specific bitwise (32 bits either zero or one) expression.

Operator Sign Description
Bitwise AND & Return bitwise AND operation for given two operands.
Bitwise OR | Return bitwise OR operation for given two operands.
Bitwise XOR ^ Return bitwise XOR operation for given two operands.
Bitwise NOT ~ Return bitwise NOT operation for given operand.
Bitwise Shift Left << Return left shift of given operands.
Bitwise Shift Right >> Return right shift of given operands.
Bitwise Unsigned Shift Right >>> Return right shift without consider sign of given operands.

Example:


Result:

Bitwise AND :0
Bitwise OR :15
Bitwise XOR :15
Bitwise NOT :-6
Bitwise Shift Left :40
Bitwise Shift Right :2
Unsigned Shift Right :2

JavaScript Increment/Decrement Operator

The increment/decrement operators are used to increment/decrement a variable's values of an operand

Operator Name Effect
++x Pre-increment Increments x by one, then returns x
x++ Post-increment Returns x, then increments x by one
--x Pre-decrement Decrements x by one, then returns x
x-- Post-decrement Returns x, then decrements x by one

Example:


Result:

	11

	11
	10

	11
	9

	9
	10

	9
Card image cap