Skip to main content

Command Palette

Search for a command to run...

JAVASCRIPT OPERATORS

THE BASICS YOU NEED TO KNOW

Updated
•View as Markdown
JAVASCRIPT OPERATORS

In the previous blog, we talked about variables and data types. You now know how JavaScript stores data and what kind of data it can work with. But storing data is just one part of the story.

The real question is what you do with that data?

If variables are containers, then operators are the tools that help you work with the data inside those containers. Without operators, your program would just hold values without doing anything useful with them.

let a = 10; 
let b = 5; 
console.log(a + b);

Here, + is doing the actual work. It is talking two values and combining them. This is what operators do. They perform operations on values.


What Are Operators?

Operators are symbols or keywords that perform operations on one or more values. These values are called operands.

let result = 10 + 5;
  • 10 and 5 are operands

  • + is the operator

This might look basic, but almost every line of code you will write uses operators in some form.


1. Arithmetic Operators

These are used for mathematical operations.

let a = 10;
let b = 3; 

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);

The modulus operator % gives the remainder.

Real use case

Checking even or odd numbers:

let number = 10; 
if (number % 2 === 0){
    console.log("Even");
}

Increment and Decrement

let x  = 5; 
x++;
x--;

Now here is where things get tricky.

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

Post increament returns first, then updates.

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

2. Assignment Operators

Basic assignment:

let x = 10;

Shorthand version:

x += 5;
x -= 2;
x *= 3;
x /= 2;

These make your code shorter and cleaner.


3. Comparison Operators

Used to compare values. Always return boolean.

console.log(10 > 5);
console.log(10 < 5); 
console.log(10 >= 10);
console.log(10 <= 5); 

Equality vs Strict Equality

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

Loose equality converts types.

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

Strict equality checks both type and value. Always prefer ===


4. Logical Operators

AND (&&)

if (true && true) {
    console.log("Runs");
}

OR (||)

if (false || true) {
    console.log("Runs");
}

NOT (!)

console.log(!true); //false 

Short-Circuiting

console.log(true && "Hello");
console.log(false && "Hello");
console.log(false || "Fallback");

Real-world use

let userName = "";
let display = userName || "Guest";

5. Ternary Operator

A shorter way to write conditions.

let age = 20;
let message = age > ? "Adult" : "Minor";

6. String Operations

let name = "Shikhar";
console.log("Hello" + name);
// Modern way:
console.log(`Hello ${name}`);

7. Type Coercion

JavaScript sometimes coverts types automatically.

console.log("5" + 2); //"52"
console.log("5" - 2); // 3
  • + prefers strings

  • other operators force numbers

Example:

console.log(true + 1); //2
console.log(false + 1); //1

8. Operator Precdence

JavaScript follows order of operations.

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

To control it:

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

Use brackets when in doubt.


9. typeof Operator

let value = "10"; 
console.log(typeof value);

Useful for debugging.

Putting It All Together

let price = 100;
let discount = 20;
let isMember = true;

let finalPrice = price - discount;
finalPrice = isMember ? finalPrice - 10 : finalPrice;
console.log(finalPrice); 

Common Mistakes

  • Using == instead of ===

  • Ignoring operator precedence

  • Misunderstanding type coercion

  • confusing increment behavior


Final Thought

Operators are everywhere.

You won't notice them when they work correctly, but the moment you misunderstand one small behaviour, your breaks. That is whey it is important to not just know operators, but understand how they behave.

JavaScript Fundamentals (The Absolute Basics )

Part 2 of 5

“Learn JavaScript from scratch with this beginner-friendly series covering variables, functions, arrays, objects, and core fundamentals with practical examples.”

Up next

Control Flow in JavaScript

If, Else, and Switch Explained