JavaScript Operators

In this tutorial, you will learn about different operators available in JavaScript and how to use them with the help of examples.

What is an Operator?

In JavaScript, an operator is a special symbol used to perform operations on operands (values and variables). For example,

Here + is an operator that performs addition, and 2 and 3 are operands.

JavaScript Operator Types

Here is a list of different operators you will learn in this tutorial.

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • String Operators
  • Other Operators

JavaScript Assignment Operators

Assignment operators are used to assign values to variables. For example,

Here, the = operator is used to assign value 10 to variable x.

Here’s a list of commonly used assignment operators:

OperatorNameExample
=Assignment operatora = 7; // 7
+=Addition assignmenta += 5; // a = a + 5
-=Subtraction Assignmenta -= 2; // a = a - 2
*=Multiplication Assignmenta *= 3; // a = a * 3
/=Division Assignmenta /= 2; // a = a / 2
%=Remainder Assignmenta %= 2; // a = a % 2
**=Exponentiation Assignmenta **= 2; // a = a**2

Note: The commonly used assignment operator is =. You will understand other assignment operators such as +=-=*= etc. once we learn arithmetic operators.

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic calculations. For example,

Here, the + operator is used to add two operands.

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Remainderx % y
++Increment (increments by 1)++x or x++
--Decrement (decrements by 1)--x or x--
**Exponentiation (Power)x ** y

Example 1: Arithmetic operators in JavaScript

Try This Code:

Output:

JavaScript Comparison Operators

Comparison operators compare two values and return a boolean value, either true or false. For example,

OperatorNameExampleResult
==Equalx == yTrue if x is equal to y
===Identicalx === yTrue if x is equal to y, and they are of the same type
!=Not equalx != yTrue if x is not equal to y
!==Not identicalx !== yTrue if x is not equal to y, or they are not of the same type
<Less thanx < yTrue if x is less than y
>Greater thanx > yTrue if x is greater than y
>=Greater than or equal tox >= yTrue if x is greater than or equal to y
<=Less than or equal tox <= yTrue if x is less than or equal to y

Try This Code:

Output:

Comparison operators are used in decision-making and loops. You will learn about the use of comparison operators in detail in later tutorials.

JavaScript Logical Operators

Logical operators perform logical operations and return a boolean value, either true or false. For example,

Here, && is the logical operator AND. Since both x < 6 and y < 5 are true, the result is true.

OperatorNameExampleResult
&&Andx && yTrue if both x and y are true
||Orx || yTrue if either x or y is true
!Not!xTrue if x is not true

The following example will show you how these logical operators actually work:

Try This Code:

Output:

2016 is a leap year.

JavaScript Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShifts left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>>Zero fill right shiftShifts right by pushing zeros in from the lef

JavaScript String Operators

In JavaScript, you can also use the + operator to concatenate (join) two or more strings.

Example 4: String operators in JavaScript

Try This Code:

Note: When + is used with strings, it performs concatenation. However, when + is used with numbers, it performs addition.

Other JavaScript Operators

Here’s a list of other operators available in JavaScript. You will learn about these operators in later tutorials.

OperatorDescriptionExample
,evaluates multiple operands and returns the value of the last operand.let a = (1, 3 , 4); // 4
?:returns value based on the condition(5 > 3) ? 'success' : 'error'; // "success"
deletedeletes an object’s property, or an element of an arraydelete x
typeofreturns a string indicating the data typetypeof 3; // "number"
voiddiscards the expression’s return valuevoid(x)
inreturns true if the specified property is in the objectprop in object
instanceofreturns true if the specified object is of of the specified object typeobject instanceof object_type
Share on:

Free Exclusive Traffic Tips

Enter your email below to get access to my
proven SEO and traffic tips.

Hello, I am Dharmendra Yadav and I am a Blogger, Web Developer, Web Designer, Search Engine Optimizer, Digital Marketing Instructor. You can find me on Facebook , Linkedin

Leave a Comment