Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators basics

(Mango Shopping Edition)

Updated
•6 min read•View as Markdown
JavaScript Operators basics

Mango season is here
When you go to a street vendor to buy mangoes, you naturally perform a few actions:

  • You add prices

  • You compare varieties

  • You decide if the deal is good

  • You update the total bill

Interestingly, these same actions exist in programming as operators.

JavaScript operators help us perform calculations, comparisons, decisions, and value updates in code.
To make this easy to understand, we’ll use a mango shopping analogy throughout the article.

1. What Are Operators?

In JavaScript, operators are symbols used to perform operations on values.

These values are called operands.

Simple Example

console.log(5 + 3);
//5 and 3 → operands 
// + → operator
// 8 → result

Mango Example

Imagine you bought: 5 kg mangoes,3 kg mangoes

So the + operator adds values together and sum is 8kg..

2. Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

They are commonly used when calculating prices, totals, or quantities.

Example

let pricePerKg = 120;
let quantity = 3;

console.log(pricePerKg + 50);      // 170 → premium mango price
console.log(pricePerKg - 20);      // 100 → after bargaining
console.log(pricePerKg * quantity); // 360 → total bill
console.log(pricePerKg / 2);       // 60 → price for half kg
console.log(pricePerKg % 50);      // 20 → remaining change

Common Arithmetic Operators

Operator Meaning Example
+ Addition Add mango quantities
- Subtraction Reduce price after bargaining
* Multiplication Calculate total bill
/ Division Split mango price
% Modulus Find remaining change

3. Comparison Operators

Comparison operators are used to compare two values.

They return a Boolean value: true/ false

Example

console.log(120 > 80);   //true
console.log(120 < 80);   //false
console.log(120 != 80);  // true

Real Life Mango Situation

You may compare prices between vendors:

  • Is Vendor A cheaper than Vendor B?

  • Are both selling the same price?

Comparison operators help answer these questions.

4. Difference Between and =

This is one of the most important concepts in JavaScript.

Loose Equality (==)

console.log(120 == "120");//gives true
//ohhhh strange right, but js auto paser the string to number

Strict Equality (===)

console.log(120 === "120"); // this false

Why?

Because:

  • 120 → number

  • "120" → string

Strict equality checks both value AND data type.

Best Practice use === , Because it avoids unexpected bugs.

5. Logical Operators

Logical operators help make decisions based on multiple conditions.

Common Logical Operators

Operator Meaning
&& AND
! NOT

Example

let price = 90;
let isRipe = true;

console.log(price < 100 && isRipe); // true
console.log(price < 100 || isRipe); // true
console.log(!isRipe); //false

Mango Buying Logic

You will buy mangoes if:

  • price is low

  • mangoes are ripe

price < 100 && isRipe // output: Both conditions must be true.

6. Assignment Operators

Assignment operators are used to assign or update values in variables.

Example

let totalBill = 0;

totalBill += 360; // this is same as :totalBill = totalBill + 360
totalBill -= 50;

console.log(totalBill);

Real Life Example

When you add mangoes to your basket:

  • The total bill increases

  • If you bargain, the bill decreases

Assignment operators help update these values.

Quick Summary Table

Category Operators Purpose
Arithmetic + - * / % Perform calculations
Comparison == === != > < Compare values
Logical &&
Assignment = += -= Update variables

Mini Project: Mango Shopping Logic

Try running this in your browser console.

let pricePerKg = 120;
let quantity = 3;

let total = pricePerKg * quantity;

console.log(total == "360");
console.log(total === "360");

let isRipe = true;
let inSeason = true;

if (total < 400 && isRipe && inSeason) {
  console.log("Perfect mango deal – Buy it! ");
} else {
  console.log("Not worth buying this time.");
}

Try modifying:

  • price

  • quantity

  • ripeness

Observe how the output changes.

This is exactly how real applications make decisions.

*Bonus topic

7. Nullish Values in JavaScript (When Mango Info is Missing)

Sometimes while buying mangoes, you may not have complete information.

For example:

  • The vendor hasn't told the price yet

  • The quantity is unknown

  • The value is missing or not defined

In JavaScript, these situations are represented by nullish values.

The two main nullish values are:

  • null

  • undefined

null → Intentional Empty Value

null means a value exists but it is intentionally empty.

Example (Mango Shop):

The vendor hasn't decided the price yet.

let mangoPrice = null;

console.log(mangoPrice); // null

It means: The price variable exists, but the value is intentionally empty.

Interesting behavior:

console.log(null + 1); // 1
//oookkk look what happend
// null → 0
// 0 + 1 → 1

undefined → Value Not Assigned

undefined means a variable exists but no value was assigned yet.

Example:

let mangoQuantity;

console.log(mangoQuantity); // undefined

Meaning: We created the variable but never gave it a value.

Now see what happens in math:

console.log(undefined + 1); // NaN
// undefined cannot convert properly to a number

What is NaN?

NaN means Not a Number.

Example:

console.log(undefined + 1); // NaN

Strange JavaScript behavior:

console.log(typeof NaN); // "number"

Even though it means Not a Number, JavaScript still treats it as a number type.

Another strange rule:

console.log(NaN === NaN); // false

Yes, NaN is the only value in JavaScript that is NOT equal to itself.

Correct way to check NaN:

console.log(Number.isNaN(NaN)); // true

Mango Analogy Summary

Situation JavaScript Value Mango Example
Price intentionally not decided null Vendor hasn't fixed the price yet
Value never assigned undefined You forgot to ask the quantity
Invalid math result NaN Trying to calculate price without numbers

What We Learned So Far :

JavaScript operators may look small, but they are fundamental building blocks of programming.

They allow developers to:

  • perform calculations

  • update values

  • compare data

  • build decision logic

  • update application state

From something as simple as calculating mango prices to complex e-commerce systems, operators are used everywhere.

Once you understand these basics, you are ready to build:

  • price calculators

  • shopping carts

  • login validation systems

  • interactive web applications

So the next time you buy mangoes from a street vendor, remember — you’re already thinking like a programmer.

Happy coding and happy mango season!