 JavaScript

# Operators in JavaScript

In real life you’ve seen how we add, subtract two values. Similarly, in a programming language, this action is performed through the help of operators. In simple words, mathematical or logical operations can be performed through the use of operators. Operators perform these functions on single or multiple operands to produce results. Here, operands are the data values on which operations are being performed.

Like any other programming language, JavaScript also uses various operators. It’s helpful as it makes the task of comparing and manipulating values easy for the user.

In this article, we’ll discuss various operators in JavaScript along with their usage and examples.

## Type of operators in JavaScript

There are various operators available in JavaScript, some are listed below:

Let’s understand these operators one by one with examples for better understanding.

## Arithmetic Operators

Arithmetic operators help in performing all the mathematical basic calculations like addition, subtraction, multiplication, and division. Following are various Arithmetic operators:

Operators Name
Subtraction
/ Division
* Multiplication
% Remainder
++ Increase by 1
Decrease by 1
** Exponential (Power)

Example of Arithmetic Operators

let x = 3;
let y = 2;

console.log('x + y = ', x + y);  // 5

// subtraction
console.log('x - y = ', x - y);  // 1

// multiplication
console.log('x * y = ', x * y);  // 6

// division
console.log('x / y = ', x / y);  // 1.5

// remainder of 3 / 2 will be 1
console.log('x % y = ', x % y);  // 1

// increase by 1
console.log('++x = ', ++x); // x will be 4
console.log('x++ = ', x++); // prints 4 and then increased to 5
console.log('x = ', x);     // 5

// decrease by 1
console.log('--x = ', --x); // x will be 4
console.log('x-- = ', x--); // prints 4 and then decreased to 3
console.log('x = ', x);     // 3

//exponentiation
console.log('x ** y =', x ** y); // 3 power of 2 is 9

## Assignment Operators

In Javascript, assignment operators are used to assign some values to a variable. For example:

let x = 3;
const y = 5;

Here, x and y are variables that are assigned values 3 and 5 respectively. Following are some assignment operators:

Operators Name
= Assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division Assignment
%= Remainder assignment
**= Exponentiation assignment

Example of Assignment Operators

let x = 3; // Assigning value using assignment operator

console.log('x += 3', x += 3);  // 6, now x = 6

// subtraction assignment
console.log('x -= 2 ', x -= 2);  // 4, now x = 4

// multiplication assignment
console.log('x *= 2 ', x *= 2);  // 8, now x = 8

// division assignment
console.log('x /= 2 ', x /= 2);  // 4

// remainder assignment
console.log('x %= 2 ', x %= 2);   // 0

//exponentiation assignment
let x = 5;
console.log('x **= 2', x **= 2); // 25

## Comparison Operators

Comparison Operators work by comparing two values and returning true and false:

Operators Name
== Equal To
!= Not equals to
=== Strict equals to
!== Strict not equal to
> Greater than
< Less than
>= Greater than or equals to
<= Less than or equals to

Example of Comparison Operators:

// equal operator
console.log(3 == 2); // false
console.log(3 == '3'); // true

// not equal operator
console.log(3 != 4); // true
console.log('world' != 'world'); // false

// strict equal operator
console.log(7 === '7'); // false
console.log(6 === 6); // true

// strict not equal operator
console.log(8 !== '8'); // true
console.log(3 !== 3); // false

## Logical Operators

Logical operators are used to handle logical operations on data values, as a result, they return a boolean value:

Operators Name
&& Logical AND
|| Logical OR
! Logical NOT

Example of Logical Operators:

// logical AND
console.log(7==7 && 7==7); // true, AND operator works when both given statements are true
console.log(7==7 && 7==87!=7); // false, one of the statement is not true

// logical OR
console.log(7==7 || 7!=7); // true, OR operator works when one of the given two statements is true

// logical NOT
console.log(!7==7); // false, would’ve been true if the expression was not true.

## String Operators

String Operators are used to join two or more strings or a number and a string.

Example of String Operators:

let x = 5 + 25; // 30
let y = "5" + 5; // 55
let z = "World" + 5; //World5

## Conclusion

TypeOf operators in JavaScript are used to tell the program regarding which mathematical operation should be performed. In this article, we learned various operators in JavaScript, how they are used, and their functionality. The operators help users to perform difficult tasks with ease, and logically.

Each specific operator is available for the user to implement a particular functionality in their program. I hope this helps you with a better understanding of operators in Javascript, and in general, as the core concepts in every programming language are interlinked. 