Operators

JavaScript operators are symbols that are used to perform operations on operands. For example:

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.

Types Of Operators

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Bitwise Operators
  4. Logical Operators
  5. Assignment Operators
  6. Special Operators

1) Arthmetic Operator

Two type of arithematic operator
1) Binary Operator
Operator Description Example
+ (Plus) (a+b) 10+10=20
- (Minus) (a-b) 20-10=10
* (Multiply) (a*b) 10*20=200
/ (Divide) (a/b) 10/2=5
% (Mod) (a%b) 10%2=0

2) Unary Operator
Operator Description Example
++ increment a=10;a++;Now a=11
-- Decrement a=10;a--;Now a=9

2) Relational Operator

Operator Description Example
> Greater then 20>10 = true
< Less then 20<10 = false
== Is equal to 10==20 = false
=== dentical (equal and of same type) 10==20 = false
!= Not equal to 10!=20 = true
>= Greater than or equal to 20>=10 = true
<= Less than or equal to 20<=10 = false

3) Logical Operator

Operator Description Example
&& Logical AND (10==20 && 20==33) = false
|| Logical OR (10==20 || 20==33) = false
! Logical Not !(10==20) = true

4) Assignment (Shorthand) Operator

Operator Description Example
= Assign a=10
+= Add and assign var a=10; a+=20; Now a = 30
-= Subtract and assign var a=20; a-=10; Now a = 10
*= Multiply and assign var a=10; a*=20; Now a = 200
/= Divide and assign var a=10; a/=2; Now a = 5
%= Modulus and assign var a=10; a%=2; Now a = 0
if we write a=a+b then we will also write this expression a+=b it means same

5) Bitwise Operator

Operator Description Example
& Bitwise AND (10==20 & 20==33) = false
| Bitwise OR (10==20 | 20==33) = false
^ Bitwise XOR (10==20 ^ 20==33) = false
~ Bitwise NOT (~10) = -10

5) Special Operator

Operator Description
(?:) Conditional Operator returns value based on the condition. It is like if-else.
, Comma Operator allows multiple expressions to be evaluated as single statement.
typeof checks the type of object.

Assignment

1) Write a JavaScript program to compute the sum of the three numbers.

2) Write a JavaScript program to compute the mul of the five numbers.

3) Write a JavaScript program to accept two numbers and display sum, mul, div and sub.

4) Write a JavaScript program to accept a student's marks in Physics,Chemistry, and Biology. The total of these marks as well as the average should be displayed.

5) Write a JavaScript program to accept 5 numbers from the user and display average.

6) Write a JavaScript program to accept two numbers from the user and swap their values with third variable.

7) Write a JavaScript program to accept one number from the user and display square.

8) Write a JavaScript program to accept one number from the user and display cube.

9) Write a JavaScript program to accept basic salary, hra,ta,da,esi,pf from the user and calculate net salary.
[Hint: (basic+hra+ta+da)-(esi+pf).

10) Write a JavaScript program to accept shopping price from the user and display net amount to pay after 10% discount.