JavaScript Operators

Introduction

In programming, operators are the building blocks that allow you to perform operations on data. Whether you’re adding numbers, comparing values, or making decisions, operators play a crucial role in JavaScript.

JavaScript provides a wide range of operators that help developers perform calculations, manipulate data, and control program flow. Understanding these operators is essential for writing efficient and bug-free code.

In this guide, you’ll learn everything about JavaScript operators, including types, syntax, examples, real-life use cases, common mistakes, and interview questions.

What are JavaScript Operators?

JavaScript operators are symbols or keywords used to perform operations on variables and values.

Example:


let sum = 10 + 5;

In the above example:

  • + is an operator
  • 10 and 5 are operands

What is the use of Operators?

Operators are essential because they:

  1. Perform calculations
  2. Compare values
  3. Assign data
  4. Control logic
  5. Simplify code

Without operators, programming would be extremely limited.

Types of JavaScript Operators

JavaScript provides several types of operators:

1. Arithmetic Operators

It is used to perform mathematical calculations.

Operator Description Example
+ Addition 5 + 2
Subtraction 5 – 2
* Multiplication 5 * 2
/ Division 5 / 2
% Modulus 5 % 2
** Exponentiation 2 ** 3

Example:


let result = 10 + 5;
console.log(result); // 15

2. Assignment Operators

It is used to assign values to variables.

Operator Example Meaning
= x = 5 Assign
+= x += 2 x = x + 2
-= x -= 2 x = x – 2
*= x *= 2 x = x * 2
/= x /= 2 x = x / 2

Example:


let x = 10;
x += 5;
console.log(x); // 15

3. Comparison Operators

It is used to compare values and return a boolean (true or false).

Operator Description
== Equal to
=== Strict equal
!= Not equal
!== Strict not equal
> Greater than
< Less than

Example:


console.log(5 == "5");  // true
console.log(5 === "5"); // false

4. Logical Operators

It is used to combine conditions.

Operator Description
&& AND
|| OR
! NOT

Example:


let age = 20;
let hasID = true;

if (age > 18 && hasID) {
   console.log("Allowed");
}

Example: Login Validation


if (username === "admin" && password === "123") {
   console.log("Login Successful");
}

5. Unary Operators

Operate on a single operand.

Operator Description
++ Increment
Decrement
typeof Type check

Example:


let x = 10;
x++;
console.log(x); // 11

6. Ternary Operator

It is a shorthand for if-else.

Syntax:


condition ? value1 : value2;

Example:


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

7. Bitwise Operators

Operate on binary numbers.

Operator Description
& AND
^ XOR
~ NOT
<< Left shift
>> Right shift

8. String Operators

It is used to work with strings.

Example:


let text = "Hello" + " World";
console.log(text);

9. Nullish Coalescing Operator (??)

It returns the right-hand value if the left is null or undefined.

It is used through ??

Example:


let value = null ?? "Default";
console.log(value); // Default

10. Optional Chaining (?.)

Safely accesses nested object properties.

Example:


let user = {};
console.log(user?.name); // undefined

11. Operator Precedence

Operator precedence determines the order in which operations are performed.

Example:


let result = 10 + 5 * 2;
console.log(result); // 20

Note: Multiplication is performed before addition.

Common Mistakes

I will show you some common mistakes that mostly happened by Fresher.

1. Using == Instead of ===

You should use === instead of == when you are comparing the variable’s values.

Example:


5 == "5"; // true (unexpected)

2. Confusing + Operator

+ operator confuse when you add string number to numeric number then it does not add number, it’s work like a string.

Example:


console.log("5" + 2); // Output is "52"

Interview Questions

Q 1: What are operators in JavaScript?
Ans: Symbols used to perform operations on values and variables.
Q 2: What are arithmetic operators?
Ans: Operators used for mathematical calculations.
Q 3: Difference between == and ===?
Ans: == allows type conversion, === does not.
Q 4: What is a ternary operator?
Ans: A shorthand for if-else statements.
Q 5: What are logical operators?
Ans: Operators used to combine conditions (&&, ||, !).
Q 6: What is the nullish coalescing operator?
Ans: Returns a default value for null or undefined.

Conclusion

JavaScript operators are essential for performing calculations, making decisions, and manipulating data. From basic arithmetic to advanced logical operations, they are used in almost every line of code.

By understanding different types of operators and their behavior, you can write cleaner, more efficient, and bug-free programs.

Related JavaScript Tutorials