Skip to main content

Operator in JavaScript

Operators are symbols that perform operations on operands. JavaScript supports various types of operators, including arithmetic, assignment, comparison, logical, and more. Let's explore some common operators used in JavaScript:

1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical calculations.

Addition (+):

The addition operator is used to add two operands.

let sum = 10 + 5; // sum = 15

Subtraction (-):

The subtraction operator is used to subtract the second operand from the first.

let difference = 20 - 8; // difference = 12

Multiplication (*):

The multiplication operator is used to multiply two operands.

let product = 5 * 4; // product = 20

Division (/):

The division operator is used to divide the first operand by the second.

let quotient = 50 / 10; // quotient = 5

Modulus (%):

The modulus operator returns the remainder of the division operation.

let remainder = 15 % 4; // remainder = 3

2. Assignment Operators:

Assignment operators are used to assign values to variables.

Assignment (=):

The assignment operator assigns the value on the right to the variable on the left.

let x = 10; // x = 10

Addition Assignment (+=):

The addition assignment operator adds the value on the right to the variable's current value.

let y = 5;
y += 3; // y = 8

Subtraction Assignment (-=):

The subtraction assignment operator subtracts the value on the right from the variable's current value.

let z = 10;
z -= 2; // z = 8

3. Comparison Operators:

Comparison operators are used to compare two values.

Equal (==):

The equal operator checks if two values are equal. But it does not consider the data type.

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

Strict Equal (===):

The strict equal operator checks if two values are equal and of the same data type.

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

Not Equal (!=):

The not equal operator checks if two values are not equal.

let isNotEqual = 10 != 5; // isNotEqual = true

Greater Than (>):

The greater than operator checks if the left operand is greater than the right operand.

let isGreaterThan = 15 > 10; // isGreaterThan = true

Less Than (<):

The less than operator checks if the left operand is less than the right operand.

let isLessThan = 5 < 10; // isLessThan = true

4. Logical Operators:

Logical operators are used to combine multiple conditions.

Logical AND (&&):

The logical AND operator returns true if both conditions are true.

let condition1 = true;
let condition2 = false;

let result = condition1 && condition2; // result = false

Logical OR (||):

The logical OR operator returns true if at least one condition is true.

let condition1 = true;
let condition2 = false;

let result = condition1 || condition2; // result = true

Logical NOT (!):

The logical NOT operator returns the opposite of the condition.

let condition = true;

let result = !condition; // result = false

5. Ternary Operator:

The ternary operator is a shorthand for an if...else statement.

let age = 20;

let message = age >= 18 ? 'You can vote' : 'You are too young to vote';

6. Typeof Operator:

The typeof operator is used to determine the data type of a variable.

let num = 10;
let str = 'Hello, World!';

console.log(typeof num); // number
console.log(typeof str); // string

7. Increment and Decrement Operators:

Increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1.

let count = 5;

count++; // count = 6
count--; // count = 5

8. Bitwise Operators:

Bitwise operators perform operations on binary representations of numbers.

Bitwise AND (&):

Performs a bitwise AND operation on two numbers.

let result = 5 & 3; // result = 1

Bitwise OR (|):

Performs a bitwise OR operation on two numbers.

let result = 5 | 3; // result = 7

Bitwise XOR (^):

Performs a bitwise XOR operation on two numbers.

let result = 5 ^ 3; // result = 6

Bitwise NOT (~):

Performs a bitwise NOT operation on a number.

let result = ~5; // result = -6

Left Shift (<<):

Shifts the bits of a number to the left.

let result = 5 << 1; // result = 10

Right Shift (>>):

Shifts the bits of a number to the right.

let result = 5 >> 1; // result = 2

Zero-fill Right Shift (>>>):

Shifts the bits of a number to the right, filling the leftmost bits with zeros.

let result = 5 >>> 1; // result = 2

9. Conditional Operator:

The conditional operator (?:) is a ternary operator that evaluates a condition and returns one of two values based on the result.

let age = 20;

let message = age >= 18 ? 'You can vote' : 'You are too young to vote';

10. Comma Operator:

The comma operator allows multiple expressions to be evaluated in a single statement.

let x = 1, y = 2, z = 3;

11. Void Operator:

The void operator evaluates an expression and returns undefined.

let result = void 0; // result = undefined

12. Delete Operator:

The delete operator is used to delete an object's property.

let person = {
name: 'John',
age: 30
};

delete person.age; // Removes the 'age' property

13. In Operator:

The in operator checks if a property exists in an object.

let person = {
name: 'John',
age: 30
};

let hasAge = 'age' in person; // hasAge = true

14. Instanceof Operator:

The instanceof operator checks if an object is an instance of a specific class.

let date = new Date();

let isDate = date instanceof Date; // isDate = true

More Resources:

Operators are an essential part of JavaScript programming. Understanding how to use them effectively will help you write more efficient and concise code. Experiment with different operators to see how they work and how you can use them in your projects.