close

DEV Community

ABISHEK M
ABISHEK M

Posted on

Operators in JavaScript

What are Operators?

Operators are special symbols or keywords used to perform operations on values and variables in JavaScript.

For example:

let a = 10;
let b = 5;

let result = a + b;
Enter fullscreen mode Exit fullscreen mode

Here, + is an operator. It is used to add the values of a and b.

JavaScript provides different types of operators for performing calculations, assigning values, comparing values, checking conditions, and more.

Types of Operators in JavaScript

The commonly used operators in JavaScript are:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Increment and Decrement Operators
  6. Ternary Operator
  7. Bitwise Operators
  8. Nullish Coalescing Operator
  9. Optional Chaining Operator
  10. Type Operators

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

let a = 10;
let b = 5;

console.log(a + b);  // 15
console.log(a - b);  // 5
console.log(a * b);  // 50
console.log(a / b);  // 2
console.log(a % b);  // 0
console.log(2 ** 3); // 8
Enter fullscreen mode Exit fullscreen mode

Common Arithmetic Operators

  • + → Addition
  • - → Subtraction
  • * → Multiplication
  • / → Division
  • % → Remainder
  • ** → Exponentiation

How it works

Arithmetic operators take numerical values and perform mathematical operations on them.

For example:

let price = 500;
let quantity = 3;

let total = price * quantity;
Enter fullscreen mode Exit fullscreen mode

Here, * multiplies the price by the quantity:

500 × 3 = 1500
Enter fullscreen mode Exit fullscreen mode

2. Assignment Operators

Assignment operators are used to assign a value to a variable or update its existing value.

let balance = 1000;
Enter fullscreen mode Exit fullscreen mode

Here, = assigns 1000 to balance.

Common Assignment Operators

  • = → Assign
  • += → Add and assign
  • -= → Subtract and assign
  • *= → Multiply and assign
  • /= → Divide and assign
  • %= → Remainder and assign

For example:

let balance = 1000;

balance += 500;
Enter fullscreen mode Exit fullscreen mode

This is the same as:

balance = balance + 500;
Enter fullscreen mode Exit fullscreen mode

The new balance becomes 1500.


3. Comparison Operators

Comparison operators are used to compare two values. They return either true or false.

let age = 20;

console.log(age > 18);   // true
console.log(age < 18);   // false
console.log(age === 20); // true
Enter fullscreen mode Exit fullscreen mode

Common Comparison Operators

  • == → Equal
  • === → Strictly equal
  • != → Not equal
  • !== → Strictly not equal
  • > → Greater than
  • < → Less than
  • >= → Greater than or equal
  • <= → Less than or equal

Important Difference

5 == "5";   // true
5 === "5";  // false
Enter fullscreen mode Exit fullscreen mode

== allows type conversion, while === checks both the value and the data type.


4. Logical Operators

Logical operators are used to combine or reverse conditions.

There are three main logical operators:

  • && → AND
  • || → OR
  • ! → NOT

AND &&

Both conditions must be true.

let age = 25;
let hasTicket = true;

console.log(age >= 18 && hasTicket);
Enter fullscreen mode Exit fullscreen mode

Both conditions are true, so the result is true.

OR ||

At least one condition must be true.

let hasEmail = true;
let hasPhone = false;

console.log(hasEmail || hasPhone);
Enter fullscreen mode Exit fullscreen mode

The result is true because hasEmail is true.

NOT !

The ! operator reverses a Boolean value.

let isLoggedIn = true;

console.log(!isLoggedIn);
Enter fullscreen mode Exit fullscreen mode

Output:

false
Enter fullscreen mode Exit fullscreen mode

Real-world usage

Logical operators are commonly used for:

  • Login systems
  • Permission checking
  • Form validation
  • Access control
  • Multiple conditions

5. Increment and Decrement Operators

The ++ operator increases a value by 1, while the -- operator decreases a value by 1.

let count = 5;

count++;
console.log(count); // 6

count--;
console.log(count); // 5
Enter fullscreen mode Exit fullscreen mode

How it works

count++;
Enter fullscreen mode Exit fullscreen mode

is similar to:

count = count + 1;
Enter fullscreen mode Exit fullscreen mode

And:

count--;
Enter fullscreen mode Exit fullscreen mode

is similar to:

count = count - 1;
Enter fullscreen mode Exit fullscreen mode

6. Ternary Operator

The ternary operator is a short way of writing a simple if...else condition.

Its syntax is:

condition ? valueIfTrue : valueIfFalse;
Enter fullscreen mode Exit fullscreen mode

Example:

let age = 20;

let result = age >= 18 ? "Adult" : "Minor";

console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

Adult
Enter fullscreen mode Exit fullscreen mode

How it works

First, JavaScript checks:

age >= 18
Enter fullscreen mode Exit fullscreen mode

If it is true, it returns "Adult".

If it is false, it returns "Minor".

\

7. Bitwise Operators

Bitwise operators work with the binary representation of numbers.

Common bitwise operators include:

  • & → Bitwise AND
  • | → Bitwise OR
  • ^ → Bitwise XOR
  • ~ → Bitwise NOT
  • << → Left shift
  • >> → Right shift
  • >>> → Unsigned right shift

For example:

let a = 5;
let b = 3;

console.log(a & b);
Enter fullscreen mode Exit fullscreen mode

JavaScript converts the numbers into binary and performs the operation on their bits.

Bitwise operators are mainly used in specific situations such as low-level programming, binary operations, flags, and certain performance-related tasks.

Beginners usually do not need to use these operators frequently in everyday JavaScript development.


8. Nullish Coalescing Operator

The ?? operator is used to provide a default value when the left side is null or undefined.

let username = null;

let name = username ?? "Guest";

console.log(name);
Enter fullscreen mode Exit fullscreen mode

Output:

Guest
Enter fullscreen mode Exit fullscreen mode

Here, username is null, so JavaScript uses "Guest".

If the value exists:

let username = "Abishek";

let name = username ?? "Guest";

console.log(name);
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
Enter fullscreen mode Exit fullscreen mode

9. Optional Chaining Operator

The ?. operator is used to safely access properties or methods when a value might be null or undefined.

Consider:

let user = {};
Enter fullscreen mode Exit fullscreen mode

If we try:

console.log(user.address.city);
Enter fullscreen mode Exit fullscreen mode

JavaScript can produce an error because address does not exist.

Instead, we can use:

console.log(user.address?.city);
Enter fullscreen mode Exit fullscreen mode

The result will be:

undefined
Enter fullscreen mode Exit fullscreen mode

How it works

The ?. operator checks whether the value before it exists.

If it exists, JavaScript continues accessing the property.

If it is null or undefined, JavaScript stops and returns undefined instead of throwing an error.


10. Type Operators

JavaScript provides operators that help us check or work with types and objects.

typeof

typeof is used to find the data type of a value.

let age = 25;

console.log(typeof age);
Enter fullscreen mode Exit fullscreen mode

Output:

number
Enter fullscreen mode Exit fullscreen mode

Another example:

let name = "Abishek";

console.log(typeof name);
Enter fullscreen mode Exit fullscreen mode

Output:

string
Enter fullscreen mode Exit fullscreen mode

Real-world usage

typeof is useful when we need to check the type of a value.


Conclusion

Operators are an important part of JavaScript because they allow us to perform calculations, assign and update values, compare values, combine conditions, make decisions, work with objects, and handle optional data.

The main operator types are:

  • Arithmetic → Perform calculations
  • Assignment → Assign or update values
  • Comparison → Compare values
  • Logical → Combine conditions
  • Increment/Decrement → Increase or decrease values
  • Ternary → Write simple conditions
  • Bitwise → Work with binary values
  • Nullish Coalescing → Provide defaults for null or undefined
  • Optional Chaining → Safely access nested values
  • Type Operators → Check types and object relationships

Top comments (0)