Javascript Operators


One can use javascript operators to perform varius type of tasks like addition,subtraction,comparison etc.

    Types of operators in javascript:

  • Arithmetic Operators-These constitute of operators which are used to peform arithmetical tasks.
  • Comparison operators-These constitute such operators which are used perform comparison.
  • Logical Operators-Logical operators are those which are used to logical operations.
  • Assignment Operators
  • Conditional Operators-Conditional operators are those operators which are used to peform conditional operations.
You will be learning about these in next chapter.

Type of arithmetic operators in javascript

Operator Description Example
+ For adding two operands. A+B
- Subtraction A-B
* Multiplication A*B
/ Division A/B
% Modular division(Remainder) Remainder of A/B
++ Incrementation A++ means A+1
-- Decrementation A- means A-1

Examples of Arithmetic Operations

Addition(1)

<script>
var x=20+30;
</script>

Addition(2)

<script>
var x=20;
var y=30;
var z=x+y;
</script>

Subtraction(1)

<script>
var x=30-20;
</script>

Subtraction(2)

<script>
var x=30;
var y=20;
var z=x-y;
</script>

Multiplication(1)

<script>
var x=30*20;
</script>

Multiplication(1)

<script>
var x=30;
var y=20;
var z=x*y;
</script>

Division(1)

<script>
var x=300/10;
</script>

Division(2)

<script>
var x=300;
var y=10;
var z=x/y;
</script>

Modular Division(1)

<script>
var x=5%4;
</script>

Modular Division(2)

<script>
var x=5;
var y=4;
var z=x%y;
</script>

Incrementation

<script>
var x=5;
var y=x++;
</script>

Decrementation

<script>
var x=5;
var y=x--;
</script>

Example(1):

<!DOCTYPE html>
<html>
<head>
<title>
ARITHMETIC OPERATION
</title>
</head>
<body>
<script>
function myFunction(){
var x=5;
var y=10;
var z=x+y;
window.alert(z);
</script>
</body>
</html>

Output:

Click this button for output.

Example(2):

<!DOCTYPE html>
<html>
<head>
<title>
ARITHMETIC OPERATION
</title>
</head>
<body>
<script>
function myFunction(){
var x=10+5;
window.alert(x);
</script>
</body>
</html>

Output:

Click this button for output.
Similarly all above mentioned examples can be seen in output.