JavaScript Operators (with Examples)

Operator and operands are two keywords used in programming to perform multiple types of operation, this operation can be of different types such as addition, subtraction, multiplication, division, etc. 

The operands are the entity to which the operation is performed. In contrast, the operators are the entity that performs that operation and decides the operation type, for example, the + operator performs addition whereas the – operator performs subtraction.

JavaScript has multiple types of operators, in this tutorial we have covered them with easy examples so that you don’t have to scratch your brain to understand them.

Types of Operators in JavaScript:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • typeof Operator
  • Ternary Operator

JavaScript Arithmetic Operators

Arithmetic Operators are the operator used to perform arithmetic operations. The arithmetic operation included addition(+), subtraction(-), multiplication(*), division(/), modulus(%), increment(++), and decrement(–).

OperatorName
+Addition
Subtraction
*Multiplication
/Division
%Modulus
++Increment
Decrement

Example:

let x = 1;
let y = 2;

let z = x + y;

console.log("Addition: " + z);

Here, we have initialised two variables with some values and then use the + operator to perform addition and print the output in the console.

Output:

Addition: 3

JavaScript Assignment Operators

Assignment Operators are used to assign value to a variable. JavaScript has many assignment operators that can perform arithmetic operators and then assign the given value such as Add and Assign, which first adds the value and then assign it.

OperatorName
=Assign
+=Add and Assign
-=Subtract and Assign
*=Multiply and Assign
/=Divide and Assign
%=Modulus and Assign

Example:

let x = 2;
let y = x; // assign value of x to y

console.log(y); 

y += y; // add y then assign total value to y

console.log(y);

Here we have used the = operator to assign the value of x to y, then used the += operator which first adds the current value of y to itself and then assigns it to itself.

Output:

2
4

JavaScript Comparison Operators

Comparison Operators are used to comparing two values(i.e. operands). There can be multiple ways someone checks for a comparison. JavaScript has many comparison operators that can perform basic to advance level comparisons.

OperatorName
==Equal
!=Not Equal
===Strict equal
!==Not Strict equal
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to

Example:

let x = 2;
let y = 3;

let compare = (x < y);

console.log(compare);

Here we have compared two variables x and y using the < operator, which returns true if the value of x is smaller than y, and returns false if it is not.

Output:

true

JavaScript Logical Operators

Logical Operators are used to perform logical AND, OR and NOT operations.

OperatorName
||Logical OR
&&Logical AND
!Logical NOT

Example:

let x = true;
let y = false;

let result = x && y;

console.log(result);

Here we have used && operator which returns true only if both side values are equal, if not then returns false.

Output:

false

JavaScript typeof Operator

This operator is used to get the type of a variable.

Example:

let x = 2;

let type = typeof x;

console.log(type);

Output:

number

Want to get the type of Object as well? Don’t worry we have a separate tutorial on it, consider reading it.

JavaScript Ternary Operator

The ternary operator is used for conditional execution. It separates a condition and two expressions with a question mark (?), and the two expressions are also separated with a colon (:), it first checks for the condition, if true then executes the first expression and, if false then executes the second expression.

Example:

let x = 2;
let y = 3;

let ternaryOperator = x < y ? "x is less than y" : "x is greater than y";

console.log(ternaryOperator);

Output:

x is less than y

Summary 

Operators in JavaScript are used to perform different types of operations on operands. In programming, we perform many operations, JavaScript operator helps exactly to do so. You can perform literally any type of operation in JavavScript using the operator we have discussed above. Hope you find this tutorial useful.

Reference 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

Read the article

Aditya Gupta
Aditya Gupta
Articles: 161