JavaScript Operators

"JavaScript operators are used to assign value,compare value,perform arithmatic operation and more."

Example- sum = a + b;
where sum , a,b are(operend) and + =(operator) .....operator and operend called Expression.

Aritmetic Operators

★  Addition →   +
★  Subtraction →   -
★  Multiplication →   *
★  Division →   /
★  Modulus (Reminder of a division) →   %
★  Increment →   ++
★  Decrement →   --

Example:-1

var a =10;
var b =20;
var sum = a+b;
document.write(sum);

Output:-

Example:-2

var a =10;
var b =30;
var sum = a+30;
document.write(sum);

Output:-

Example:-3

var a =Ganesh;
var b =Singh;
document.write(a + b);

Output:-

Assignment Operators

★  Assign →   =
★  Add and Assign (Ex..... x += y is the same as x = x+y) →   +=
★  Subtract and assign (Ex..... x -= y is the same as x = x-y) →   -=
★  Multiple and assign (Ex..... x *= y is the same as x = x*y) →   *=
★  Divide and assign (Ex..... x /= y is the same as x = x/y) →   /=
★  Modulus and assign (Ex..... x %= y is the same as x = x+y) →   %=
Note :- If two string some white space then concatinate (+ " "+) .

Example:-1

var number =10;
number = number +5;
document.write(number);

Output:-

Example:-2

var number =20;
number += 5;
document.write(number);

Output:-

Example:-3

var number =20;
number /= 5;
document.write(number);

Output:-

Conditional Statements

Conditional Statement are used to perform different action based on different condition. These are mostly two type.

If - Statement :-If Statement execute some code only if ispecified condition is True.
If-else Statement :-If-else Statement execute some code if the condition is True. and another code if the condition is False.

Example :-

var marks =95;
if (marks ==95)
{
document.write("Yes your marks is correct");
}
else
{ document.write("The value is incorrect")
}

Output:-
Switch Statement :-Switch statement is used to select one of many blocks of code to be executed.

Example :-1

Enter your choice

  • add
  • sub
  • multi


Example :-2

id="demo"
var day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
} document.getElementById("demo").innerHTML ="Today is" + day;

Output:-


Comparision Operators

★  Is Eqaul to →   = =
★ Is Identical (Is eqaul to and is of the same type) →   = = =
★  Is not eqaul to →   !=
★  Is not identical →   != =
★  Greater than →   >
★  Greater than or equal to →   >=
★  Less than →   <
★  Less than or equal to →   <=

Example:-1

var a =2;
var b="2";
if (a == b)
{ document.write("Thes Vaue is eqaul");
} else
{ document.write("The value are not equal");
}

Output:-

Note:- The is == value only comapre the value of variable.Because the a=1 is number(no code ) and the b="1" are string but the output is not equal.

Example:-2

var a =2;
var b="2";
if (a === b)
{ document.write("Thes Vaue is eqaul");
} else
{ document.write("The value are not equal");
}

Output:-

Note:- The is === value only comapre the value and data type of variable.Because the a=1 is number(no code ) and the b="1" are string but the output is same.

Example:-3

var a =5;
var b="5";
if (a != b)
{ document.write(" Both are eqaul");
} else
{ document.write("Both are not equal");
}

Output:-

Note:- The is != value only comapre the value of variable.Because the a=1 is number(no code ) and the b="1" are string but the output is same. but the !== are compare the value and data type of a variable. and output are both are equal

This site design & Develop by Ganesh singh aswal