Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

The Small Symbols That Do the Real Work in Your Code

Updated
•3 min read•View as Markdown

When we write code in JavaScript, It feels like variables and functions are doing all the heavy lifting. But in reality, operators are the ones making things happen.

They perform calculations, compare values and help your code make decisions.


What Are Operators?

Operators are special symbols that perform operations on values (called operands).

Think of them like actions:

  • + → add

  • > → compare

  • && → combine conditions

Without operators, your code would just store data—but never use it.


Arithmetic Operators

Let’s start with something familiar—basic math.

let a = 10;
let b = 3;

console.log(a + b); // 13 (addition)
console.log(a - b); // 7  (subtraction)
console.log(a * b); // 30 (multiplication)
console.log(a / b); // 3.33... (division)
console.log(a % b); // 1  (remainder)

% (modulus) gives the remainder, which is super useful (like checking even/odd numbers).


Comparison Operators

These operators compare values and return true or false.

console.log(5 > 3);   // true
console.log(5 < 3);   // false
console.log(5 == "5");  // true
console.log(5 === "5"); // false
console.log(5 != 3);  // true

== vs === (Very Important)

This is where many beginners get confused.

  • == ----> checks value only (ignores type)

  • === ----> checks value AND type

console.log(5 == "5");   // true  (string converted to number)
console.log(5 === "5");  // false (different types)

In real world projects, always prefer === for safer and predictable results.


Logical Operators

These are used when you have multiple conditions.

let age = 20;
let hasID = true;

console.log(age >= 18 && hasID); // true (both must be true)
console.log(age < 18 || hasID);  // true (at least one true)
console.log(!hasID);             // false (opposite value)

Easy way to Remember

  • && → AND → both conditions must be true

  • || → OR → at least one must be true

  • ! → NOT → flips true/false


Assignment Operators

These are used to assign or update values.

let x = 10;

x += 5;  // x = x + 5 → 15
x -= 3;  // x = x - 3 → 12

Shorter, cleaner & very commonly used.


Examples

1.Even or Odd Checker (Using %)

let number = 7;

if (number % 2 === 0) {
  console.log("Even number");
} else {
  console.log("Odd number");
}
  • % helps check remainder

  • Very common in logic building

2.Grade System (Multiple Conditions)

let marks = 82;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else if (marks >= 50) {
  console.log("Grade C");
} else {
  console.log("Fail");
}
  • >= for ranges

  • else if ladder for multiple decisions

3.Form Validation (Login Example)

let email = "user@example.com";
let password = "123456";

if (email !== "" && password.length >= 6) {
  console.log("Login successful");
} else {
  console.log("Invalid credentials");
}
  • !== --> check empty string

  • >= --> password length rule

  • && both conditions must be true


Conclusion

Operators may look small, but they are everywhere in your code.

  • Arithmetic --> for calculations

  • Comparison --> for decisions

  • Logical --> for combining conditions

  • Assignment --> for updating values


2 views