JavaScript Operators | Types of Operators

What is an Operator?

In JavaScript, an operator is a special symbol used to perform operations on operands (values and variables). An operator is an action performed on one or more operands that evaluates expressions or change the data. Operators can either be unary, binary, or ternary. For example

2 + 3; // 5

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

JavaScript Operator Types

  • 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:

const x = 5;

Here, the = operator is used to assign value 5 to variable x. Here’s a list of commonly used assignment
operators:

OperatorNameExample
=Assignment operatora = 7; // 7
+=Addition assignmenta += 5; // b= 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

JavaScript Arithmetic Operators

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

const number = 3 + 5; // 8

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

OperatorNameExample
+AdditionX+Y
SubtractionX-y
*MultiplicationX*Y
/DivisionX/y

Example 1: Arithmetic operators in JavaScript

let x = 5;
let y = 3;
// addition
console.log('x + y = ', x + y); // 8
// subtraction
console.log('x - y = ', x - y); // 2
// multiplication
console.log('x * y = ', x * y); // 15
// division
console.log('x / y = ', x / y); //
1.6666666666666667
// remainder
console.log('x % y = ', x % y); // 2
// increment
console.log('++x = ', ++x); // x is
now 6
console.log('x++ = ', x++); // prints
6 and then increased to 7
console.log('x = ', x); // 7
// decrement
console.log('--x = ', --x); // x 
is
now 6
console.log('x-- = ', x--); // prints
6 and then decreased to 5
console.log('x = ', x); // 5
//exponentiation
console.log('x ** y =', x ** y);

JavaScript Comparison Operators

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

const a = 3, b = 2;
console.log(a > b); // true

Here, the comparison operator > is used to compare whether a is greater than b.

Example 2: Comparison operators in JavaScript

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

JavaScript Logical Operators

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

const x = 5, y = 3;
(x < 6) && (y < 5); // true

Example 3: Logical Operators in JavaScript

// logical AND
console.log(true && true); // true
console.log(true && false); // false
// logical OR
console.log(true || false); // true
// logical NOT
console.log(!true); // false

Output

true
false
true
false

JavaScript Bitwise Operators

Bitwise operators perform operations on binary representations of number

OperatorDescription
&BITWISE AND
|BITWISE OR
^BITWISE XOR
~BITWISE NOT

JavaScript String Operators

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

Example 4: String operators in JavaScript

// concatenation operator
console.log('hello' + 'world');
let a = 'JavaScript';
a += ' tutorial'; // a = a + '
tutorial';
console.log(a);

Output

helloworld
JavaScript tutorial

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

JavaScript Operators | Types of Operators
Show Buttons
Hide Buttons