Javascript Operator(Part-1)

Shah Amisha
4 min readMay 16, 2023

--

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 first 2 operators here and rest are in next part(or Part 2)

(1)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

- (Subtraction) :- Subtracts the second operand from the first

Ex: A — B will give -10

* (Multiplication) :- Multiply both operands

Ex: A * B will give 200

/ (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

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

Example

The following code shows how to use arithmetic operators in JavaScript.

<html>
<body>

<script type = "text/javascript">
<!--
var a = 33;
var b = 10;
var c = "Test";
var linebreak = "<br />";

document.write("a + b = ");
result = a + b;
document.write(result);
document.write(linebreak);

document.write("a - b = ");
result = a - b;
document.write(result);
document.write(linebreak);

document.write("a / b = ");
result = a / b;
document.write(result);
document.write(linebreak);

document.write("a % b = ");
result = a % b;
document.write(result);
document.write(linebreak);

document.write("a + b + c = ");
result = a + b + c;
document.write(result);
document.write(linebreak);

a = ++a;
document.write("++a = ");
result = ++a;
document.write(result);
document.write(linebreak);

b = --b;
document.write("--b = ");
result = --b;
document.write(result);
document.write(linebreak);
//-->
</script>
</body>
</html>

Output:

a + b = 43
a - b = 23
a / b = 3.3
a % b = 3
a + b + c = 43Test
++a = 35
--b = 8

(2) 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.

!= (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

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

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

Example

The following code shows how to use comparison operators in JavaScript.

<html>
<body>
<script type = "text/javascript">
<!--
var a = 10;
var b = 20;
var linebreak = "<br />";

document.write("(a == b) => ");
result = (a == b);
document.write(result);
document.write(linebreak);

document.write("(a < b) => ");
result = (a < b);
document.write(result);
document.write(linebreak);

document.write("(a > b) => ");
result = (a > b);
document.write(result);
document.write(linebreak);

document.write("(a != b) => ");
result = (a != b);
document.write(result);
document.write(linebreak);

document.write("(a >= b) => ");
result = (a >= b);
document.write(result);
document.write(linebreak);

document.write("(a <= b) => ");
result = (a <= b);
document.write(result);
document.write(linebreak);
//-->
</script>
</body>
</html>

Output:

(a == b) => false 
(a < b) => true
(a > b) => false
(a != b) => true
(a >= b) => false
a <= b) => true

Thanks for reading, and claps are very appreciated!

If you enjoyed reading this, you may also enjoy some of my other blogs.Click here to read some of my previous stories.

--

--