What are JavaScript Operators?

What are JavaScript Operators?

4 mins read359 Views Comment
Chanchal
Chanchal Aggarwal
Senior Executive Content
Updated on Dec 27, 2023 13:35 IST

JavaScript operators are special symbols used in scripts to perform operations on operands, such as arithmetic calculations, logical comparisons, or value assignments. It plays a crucial role in controlling the flow and processing of data within the language. 

2023_01_What-are-JavaScript-Operators.jpg

Just like other programming languages, JavaScript offers various operators to perform distinct functions. JavaScript operators can be categorized into 5 types: Arithmetic, Assignment, Comparison, Logical and Conditional. Let’s know about each of them with the help of examples. 

Explore: Online JavaScript Courses and Certifications

Table of Content

What is an Operator?

JavaScript operators are symbols that are used to execute operations on operands. Operands are the variables they operate on. 

For example: 

If we add 3 and 7, we get 10 as a result.

3+7=10

In this case, 3 and 7 are operands, whereas + is an operator.

JavaScript Uses in Various Sectors
JavaScript Uses in Various Sectors
Many websites utilize the lightweight, object-oriented programming language JavaScript to write their web pages. It is a complete programming language that is interpreted. When used with HTML, JavaScript allows dynamic...read more

Introduction to JavaScript Data Types With Examples
Introduction to JavaScript Data Types With Examples
JavaScript supports several data types including numbers, strings, booleans, arrays, objects, null, and undefined. Numbers represent numeric values, strings represent text, booleans represent true or false values, arrays store multiple...read more
Recommended online courses

Best-suited JavaScript courses for you

Learn JavaScript with these high-rated online courses

2.05 K
3 months
2.2 K
4 weeks
8.5 K
62 hours
– / –
200 hours
– / –
40 hours
– / –
40 hours
– / –
40 hours
– / –
40 hours

What are JavaScript Operators?

In JavaScript, we have various types of operators. 

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

Arithmetic Operators

Arithmetic operators perform basic arithmetic operations on the operands. Below we have shown all arithmetic operators with their examples.

Operator Description Example
+ Add 7+3=10
Subtract 7-3=10
* Multiply 7*3=21
/ Divide 7/3=2.34
% Modulus (Division Remainder) 7%3=1
++ Increment (Increase answer by 1) var a=7; a ++; now a= 9
–  Decrement (Decrease answer by 1) var a=7; a – -; now a=5

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


 
<html>
<body>
<script type="text/javascript">
<!--
var a = 7;
var b = 3;
var c = "Test";
var linebreak = "<br />";
[removed]("a + b = ");
result = a + b;
[removed](result);
[removed](linebreak);
[removed]("a - b = ");
result = a - b;
[removed](result);
[removed](linebreak);
[removed]("a / b = ");
result = a / b;
[removed](result);
[removed](linebreak);
[removed]("a % b = ");
result = a % b;
[removed](result);
[removed](linebreak);
a = ++a;
[removed]("++a = ");
result = ++a;
[removed](result);
[removed](linebreak);
b = --b;
[removed]("--b = ");
result = --b;
[removed](result);
[removed](linebreak);
//-->
</script>
Set the variables to different values and then try...
</body>
</html>
Copy code

Output:

a + b = 10
a - b = 4
a / b = 2.3333333333333335
a % b = 1
++a = 9
--a = 5

Difference between Java and JavaScript | Java vs Javascript
Difference between Java and JavaScript | Java vs Javascript
Java and JavaScript are one of the two most popular programming languages that are used by developers around the globe. In this article, we will discuss the difference between two...read more

JavaScript Array – How to Use Them?
JavaScript Array – How to Use Them?
JavaScript arrays are versatile, ordered collections of elements that can hold items of any data type, including numbers, strings, or objects. They offer a range of methods for traversal, manipulation,...read more

Assignment Operators

The assignment operator assigns the value, variable, and function to another variable.

Here is the list of assignment operators.

Operator Description
= Assign
+= Add and Assign
-= Subtract and Assign
*= Multiple and Assign
/= Divide and Assign
%= Modulus and Assign

Example:


 
<html>
<body>
<script type="text/javascript">
<!--
var a = 7;
var b = 3;
var linebreak = "<br />";
[removed]("Value of a => (a = b) => ");
result = (a = b);
[removed](result);
[removed](linebreak);
[removed]("Value of a => (a += b) => ");
result = (a += b);
[removed](result);
[removed](linebreak);
[removed]("Value of a => (a -= b) => ");
result = (a -= b);
[removed](result);
[removed](linebreak);
[removed]("Value of a => (a *= b) => ");
result = (a *= b);
[removed](result);
[removed](linebreak);
[removed]("Value of a => (a /= b) => ");
result = (a /= b);
[removed](result);
[removed](linebreak);
[removed]("Value of a => (a %= b) => ");
result = (a %= b);
[removed](result);
[removed](linebreak);
//-->
</script>
</body>
</html>
Copy code

Output

Value of a => (a = b) => 3
Value of a => (a += b) => 6
Value of a => (a -= b) => 3
Value of a => (a *= b) => 9
Value of a => (a /= b) => 3
Value of a => (a %= b) => 0

Working with JavaScript Objects
Working with JavaScript Objects
JavaScript is an object-oriented programming language. In JavaScript, everything is an object. A JavaScript object is a stateful entity with behavior (properties and method). For example, a car, a pen,...read more

An Overview of JavaScript Classes
An Overview of JavaScript Classes
ES6 introduces JavaScript classes. They are templates for creating JavaScript objects. In a class, data is encapsulated with code that can be used to process it. This blog will give...read more

Comparison Operators

Comparison operators are used in logical statements to analyze equality or difference between variables or values.

Operator Description
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example:


 
<html>
<body>
<script type = "text/javascript">
<!--
var a = 7;
var b = 3;
var linebreak = "<br />";
[removed]("(a == b) => ");
result = (a == b);
[removed](result);
[removed](linebreak);
[removed]("(a < b) => ");
result = (a < b);
[removed](result);
[removed](linebreak);
[removed]("(a > b) => ");
result = (a > b);
[removed](result);
[removed](linebreak);
[removed]("(a != b) => ");
result = (a != b);
[removed](result);
[removed](linebreak);
[removed]("(a >= b) => ");
result = (a >= b);
[removed](result);
[removed](linebreak);
[removed]("(a <= b) => ");
result = (a <= b);
[removed](result);
[removed](linebreak);
//-->
</script>
</body>
</html>
Copy code

Output:

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

JavaScript var | Definition, Syntax, Characteristics and Examples
JavaScript var | Definition, Syntax, Characteristics and Examples
Have you heard of ‘var’ keyword in JavaScript? Well, in this blog we will understand everything about it starting from definition, syntax, characteristics and various examples! Let us see more...read more

Understanding JavaScript While Loop
Understanding JavaScript While Loop
The JavaScript while loop continuously executes a block of code as long as a specified condition is true, offering a way to repeat operations efficiently until a certain criterion is...read more

Logical Operators

A logical operator determines whether a Boolean expression is true or false based on the operator used.

Operator Description
&& logical AND
|| logical OR
! logical Not

 
<html>
<body>
<script type="text/javascript">
<!--
var a = true;
var b = false;
var linebreak = "<br />";
[removed]("(a && b) => ");
result = (a && b);
[removed](result);
[removed](linebreak);
[removed]("(a || b) => ");
result = (a || b);
[removed](result);
[removed](linebreak);
[removed]("!(a && b) => ");
result = (!(a && b));
[removed](result);
[removed](linebreak);
//-->
</script>
</body>
</html>
Copy code

Output:

(a && b) => false 
(a || b) => true 
!(a && b) => true

JavaScript Functions
JavaScript Functions
A JavaScript function is a block of reusable code designed to perform a particular task, enhancing code efficiency and organization. Functions are invoked or called to execute their defined operations...read more

How to Remove Duplicates from JavaScript Array?
How to Remove Duplicates from JavaScript Array?
Entering some items twice is a common problem one can face while entering data in a system. It can be duplicates of names while entering students’ names and numbers or...read more

Bitwise Operators

The bit operators operate on 32-bit numbers. In operation, any numeric operand is converted into a 32-bit number. JavaScript converts the result back to a number.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Bitwise Left Shift
>> Bitwise Right Shift

Example:


 
<html>
<body>
<script type="text/javascript">
<!--
var a = 7; // Bit presentation 10
var b = 3; // Bit presentation 11
var linebreak = "<br />";
[removed]("(a & b) => ");
result = (a & b);
[removed](result);
[removed](linebreak);
[removed]("(a | b) => ");
result = (a | b);
[removed](result);
[removed](linebreak);
[removed]("(a ^ b) => ");
result = (a ^ b);
[removed](result);
[removed](linebreak);
[removed]("(~b) => ");
result = (~b);
[removed](result);
[removed](linebreak);
[removed]("(a << b) => ");
result = (a << b);
[removed](result);
[removed](linebreak);
[removed]("(a >> b) => ");
result = (a >> b);
[removed](result);
[removed](linebreak);
//-->
</script>
</body>
</html>
Copy code

Output:

(a & b) => 3
(a | b) => 7
(a ^ b) => 4
(~b) => -4
(a << b) => 56
(a >> b) => 0

Also Read: Top JavaScript Interview Questions and Answers

Top FAQs on JavaScript Operators

What are JavaScript operators?

JavaScript operators are symbols used to perform operations on variables and values. They include arithmetic operators (like

+
Copy code
,
-
Copy code
), comparison operators (like
==
Copy code
,
>
Copy code
), logical operators (like
&&
Copy code
,
||
Copy code
), and more.

What is the difference between == and === in JavaScript?

The

==
Copy code
operator checks for equality of value but not type, which can lead to type coercion. In contrast,
===
Copy code
checks for both value and type equality, known as strict equality.

How does the && operator work in JavaScript?

The && operator is a logical AND that returns true only if both operands are true. If the first operand is false, JavaScript doesn't evaluate the second operand and returns the first false value.

What is the purpose of the typeof operator?

The typeof operator returns a string indicating the type of the unevaluated operand. It's used to find the data type of a variable, like number, string, or object.

What does the "!" operator do in JavaScript?

The "!" operator is a logical NOT. It inverts the truthiness of the operand. If used twice, like !!, it converts a value to its boolean equivalent."

About the Author
author-image
Chanchal Aggarwal
Senior Executive Content

Chanchal is a creative and enthusiastic content creator who enjoys writing research-driven, audience-specific and engaging content. Her curiosity for learning and exploring makes her a suitable writer for a variety ... Read Full Bio