Operators in JS

Operators in Java Script

An expression is a combination of operators operands that can be evaluated. It may also include function calls which return values.


x = 7.5 // a numeric literal
“Hello India!” // a string literal
false // a Boolean literal
{feet:10, inches:5} // an object literal
[2,5,6,3,5,7] // an array literal
v= m + n; // the variable v
tot // the variable tot


These are used to perform arithmetic/mathematical operations like subtraction, division, multiplication etc. Arithmetic operators work on one or more numerical values (either literals or variables) and return a single numerical value. The basic arithmetic operators are:

+ (Addition) 
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus) 


var s = 10 + 20; // result: s=30
var h = 50 * 4; // result: h = 200
var d = 100 / 4; // result: d = 25
var r = 72 % 14; // result: r=2

Increment and decrement operators

These operators are used for increasing or decreasing the value of a variable by 1. Calculations performed using these operators are very fast.

++ Increment (By One)

-- Decrement (By One)


var a = 15;
a++; // result: a = 16
var b = 20;
b—; // result: b = 19


It assigns the value of its right operand to its left operand. This operator is represented by equal sign(=).


x = 100; // This statement assigns the value 100 to x.

JavaScript also supports shorthand operator for standard operations.

The shorthand operator with example :

Shorthand operator
is equivalent to
+=a + = b
a = a + b
-=a - = b
a = a - b
*=a * = b
a = a * b
/=a / = b
a = a / b
%=a % = b
a = a % b


Relational Operators are some symbols which return a Boolean value true or false after evaluating the condition. For example x > y; returns a value true is value of variable x is greater than variable y.

Basic JavaScript comparison operators are given in the table below :

= =
is equal to
4 = = 8 returns false
! =
is not equal to
4 ! = 8 returns true
is greater than
8 > 4 returns true
is less than
8 > 4 returns false
< =
is less than or equal to
8 < = 4 returns false
> =
is greater than or equal to
8 > = 4 returns true

Relational operators are functional for strings as well. The comparison takes place in alphabetical order. This alphabetical order is based on ASCII number. For example :

“zero” < “one”
“Zero” < “one”
10 < 5
false, numeric comparison.
“10” < “5”
true, string comparison.
“10” < 5
false, numeric comparison;
“Ten” < 5
Error occurs, “Ten” can not be converted into a number


Logical operators are used for combining two or more conditions. JavaScript has following three logical operators :

Description with Example
&& (AND)
returns true if both operands are true else it return false.
| | (OR)
returns false if both operands are false else it returns true.
! (NOT)
returns true if the operand is false and false if operand is true.


The + operator concatenates two string operands. The + operator gives priority to string operands over numeric operands It works from left to right. The results depend on the order in which operations are performed.

For example :

“Good” + “Morning”
“5” + “10”
“Lucky” + 7
4 + 7 + “Delhi”
“Mumbai” + 0 +0+ 7

Conditional Operator ( ? : )

The conditional operator is a special JavaScript operator that takes three operands. Hence, it is also called ternary operator. A conditional operator assigns a value to a variable based on the condition.


var_name = (condition) ? v_1 : v_2

If (condition) is true, the value v_1 is assigned to the variable, otherwise, it assigns the value v_2 to the variable.

For example

status = (age >= 18) ? “adult” : “minor”

This statement assigns the value “adult” to the variable status if age is eighteen or more. Otherwise, it assigns the value “minor” to status.

New Operator:

new operator is used to create an instance and allocate memory to a user-defined or predefined object types.


ObjectName = new objectType ( param1 [,param2] ...[,paramN])


d = new Date(); // date assigns to object d
r = new rectangle(4, 5, 7, 8);

Delete Operator

The delete operator de-allocates (releases) the memory space that was allocated using the new operator by deleting an object, an object’s property or an element from an array.

The syntax is

delete object_name
delete object_name.property
delete array_name[index]

delete operator can be used to delete variables declared implicitly but not those declared with the var statement. The delete operator returns true if the operation is possible; it returns false if the operation is not possible.

Example Programs

Program [1]

Create a webpage and write a program in java script to show the result of student attendance on weekly basis for xyz subject. A total of 5 classes can be conducted in a week. The document should contain a form with radio buttons that allows teacher to select status of absence or presence for 5 students of a class. The page also allows to select date and time for which attendance is to be marked. On submitting attendance for a particular day it should prompt a message for successful entry. The program should display the output as :

(i) total number of classes attended out of total number of classes conducted for each student

(ii) %age of attendance

(iii) Buttons should be enabled and disabled as per conditions.

View Solution

Program [2]

Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript. 

View Solution

Program [3]

Write a JavaScript to design a simple calculator to perform the following operations: sum, product, difference and quotient.

View Solution

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Website development Company in Allahabad Sarkari Exam Quiz