JavaScript operators are special symbols or keywords that perform operations on one or more operands (data values) and produce a result. Understanding operators is fundamental for effective JavaScript programming as they are used in almost every aspect of the language, from simple arithmetic to complex logical operations.


Types of JavaScript Operators

JavaScript operators can be categorized into several types:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. String Operators
  7. Conditional (Ternary) Operator

Type Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers.


Addition (+): Adds two operands.
 
let x = 5;
let y = 10;
let result = x + y; // 15


Subtraction (-): Subtracts the second operand from the first.
 
let result = y - x; // 5
 

Multiplication (*): Multiplies two operands.
 

let result = x * y; // 50
 

Division (/): Divides the first operand by the second.
 

let result = y / x; // 2

Modulus (%): Returns the remainder of a division.

 
let result = y % x; // 0
 

Exponentiation ()**: Raises the first operand to the power of the second operand.

 
let result = x ** 2; // 25

Increment (++): Increases an integer value by one.

 
x++; // x is now 6
 

Decrement (--): Decreases an integer value by one.
 

x--; // x is now 5


2. Assignment Operators

Assignment operators are used to assign values to variables.

Assignment (=): Assigns the right operand value to the left operand.
 

let x = 10;
 

Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.

 
x += 5; // x is now 15
 

Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

 
x -= 3; // x is now 12
 

Multiplication Assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.

 
x *= 2; // x is now 24

Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
 

x /= 4; // x is now 6

Modulus Assignment (%=): Takes modulus using the two operands and assigns the result to the left operand.
 

x %= 4; // x is now 2
 

Exponentiation Assignment (=)**: Raises the left operand to the power of the right operand and assigns the result to the left operand.

 
x **= 2; // x is now 4


3. Comparison Operators

Comparison operators are used to compare two values and return a boolean value (true or false).

Equal to (==): Checks if two values are equal.
 

let isEqual = (5 == '5'); // true
 

Not equal to (!=): Checks if two values are not equal.

 
let isNotEqual = (5 != '5'); // false
 

Strict equal to (===): Checks if two values are equal in value and type.

 
let isStrictEqual = (5 === '5'); // false
 

Strict not equal to (!==): Checks if two values are not equal in value or type.
 

let isStrictNotEqual = (5 !== '5'); // true


Greater than (>): Checks if the left value is greater than the right value.

 
let isGreater = (10 > 5); // true
 

Less than (<): Checks if the left value is less than the right value.
 

let isLess = (10 < 5); // false
 

Greater than or equal to (>=): Checks if the left value is greater than or equal to the right value.
 

let isGreaterOrEqual = (10 >= 10); // true
 

Less than or equal to (<=): Checks if the left value is less than or equal to the right value.

 
let isLessOrEqual = (5 <= 10); // true


4. Logical Operators

Logical operators are used to combine or invert boolean values.

Logical AND (&&): Returns true if both operands are true.

 
let result = (true && false); // false
 

Logical OR (||): Returns true if at least one operand is true.
 

let result = (true || false); // true
 

Logical NOT (!): Inverts the boolean value of the operand.
 

let result = !true; // false


5. Bitwise Operators

Bitwise operators perform operations on the binary representations of numbers.

AND (&): Returns a 1 in each bit position where both corresponding bits are 1.

 
let result = (5 & 1); // 1 (0101 & 0001)
 

OR (|): Returns a 1 in each bit position where at least one of the corresponding bits is 1.
 

let result = (5 | 1); // 5 (0101 | 0001)
 

XOR (^): Returns a 1 in each bit position where the corresponding bits are different.
 

let result = (5 ^ 1); // 4 (0101 ^ 0001)
 

NOT (~): Inverts all the bits of the operand.
 

let result = (~5); // -6 (~0101)
 

Left Shift (<<): Shifts the bits of the operand to the left by the specified number of positions.
 

let result = (5 << 1); // 10 (0101 << 1)
 

Right Shift (>>): Shifts the bits of the operand to the right by the specified number of positions.
 

let result = (5 >> 1); // 2 (0101 >> 1)

Unsigned Right Shift (>>>): Shifts the bits of the operand to the right by the specified number of positions, filling in zeros from the left.
 

let result = (-5 >>> 1); // 2147483645


6. String Operators

String operators are used to perform operations on strings.

Concatenation (+): Joins two or more strings together.

 
let greeting = "Hello" + " " + "World!"; // "Hello World!"
 

Concatenation Assignment (+=): Appends the right operand to the left operand and assigns the result to the left operand.
 

let greeting = "Hello";
greeting += " World!"; // "Hello World!"


7. Conditional (Ternary) Operator

The conditional (ternary) operator assigns a value to a variable based on a condition.


Syntax: condition ? expressionIfTrue : expressionIfFalse

 
let isAdult = (age >= 18) ? "Yes" : "No";


8. Type Operators

Type operators are used to determine or change the type of a value.

typeof: Returns a string indicating the type of the operand.

 
let type = typeof 42; // "number"
 

instanceof: Tests whether an object is an instance of a constructor.

 
let isInstance = (new Date() instanceof Date); // true
 

void: Evaluates an expression without returning a value.
 

void(0); // undefined


 



Practice Excercise Practice now