JavaScript Operators

In this article, we will discuss Javascript Operators. There are many javascript operators. They are

  • Assignment Operator
  • Arithmetic Operator
  • Comparison Operator
  • Bitwise Operator
  • Logical Operator
  • Assignment Operator
  • Special Operator

We have already seen the Assignment operator in the previous article.

const x = 10
const y = 20
const addXY = x + y

Here in the first line, we are using the assignment operator (=) to assign a value 10 to x. In the next line, we are assigning the value 20 to y with the assignment operator (=). In the next line we are adding up the two numbers with the arithmetic addition operator (+) and the addition value is assigned to the addXY variable with the assignment operator (=).

As you have realized how to use operators now take a look in detail at the Assignment operator first.

Operators are simply used to do some operations. Below are the table of Arithmetic operators.

JavaScript Assignment Operators

OperatorExampleSame As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x – y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
**=x **= yx = x ** y

Shift Assignment Operators

OperatorExampleSame As
<<=x <<=yx = x << y
>> =x >>= yx = x >> y
>>>=x >>>= yx = x >>> y

Bitwise Assignment Operators

OperatorExampleSame As
&&=x &&= yx = x && (x = y)
||=x ||= yx = x || (x = y)
??=x ??= yx = x ?? (x = y)

Let us take a look at the operators one by one, and how they are used.

The simple Assignment operator (=) assigns a value to a variable

const x = 10

The += operator is the Addition assignment operator.

let x = 5
x += 5
console.log(x)

Run the program with node index.js. The output will be 10.

10

The -= Subsctraction Assignment Operator subtracts a value from a variable.

let x = 10
x -= 5
console.log(x)

The output will be

5

The *= Multiplication assignment operator multiplies a variable with a value.

let x = 2;
x *= 5;
console.log(x);

The output will be

10

The **= is the exponential assignment operator.

let x = 2;
x **= 5;
console.log(x);

The output will be

32

The /= is the Division Assignment Operator.

let x = 20;
x /= 5;
console.log(x);

The output will be

4

The %= is the Remainder Assignment Operator.

let x = 100;
x %= 5;
console.log(x);

The output will be

0

The <<= is the Left Shift Assignment Operator.

let x = -10;
x <<= 5;
console.log(x);

The output will be

-320

The >>= is the Right Shift Assignment Operator.

let x = -100;
x >>= 5;
console.log(x);

The output will be

-4

The >>>= is the Unsigned Right Shift Assignment Operator.

let x = -100;
x >>>= 5;
console.log(x);

The output will be

134217724

The &= is the Bitwise AND Assignment Operator.

let x = 10;
x &= 5;
console.log(x);

The output will be

4

The |= is the Bitwise OR Assignment.

let x = 100;
x |= 5;
console.log(x);

The output will be

101

The ^= is the

let x = 100;
x ^= 5;
console.log(x);

The output will be

97

The &&= is the Logical AND assignment operator

let x = 100;
x &&= 5;
console.log(x);

The output will be

5

The ||= is the Logical OR assignment operator

let x = 100;
x ||= 5;
console.log(x);

The output will be

100

The ??= is the Nullish coalescing assignment operator

let x = 100;
x ??= 5;
console.log(x);

The output will be

100

So these are the list of examples of Javascript operators. Practice it with different values. Copy and paste the code or better if you type it. See how the codes are working. In the next article, we will discuss Javascript datatypes.

Leave a Comment