Ticker

6/recent/ticker-posts

Chapter 4: Operators and Type Conversion – whosgeek

 

Chapter 4: Operators and Type Conversion – whosgeek


Imagine Running a Supermarket

Let's imagine you own a supermarket.

Throughout the day you perform many operations.

Examples:

Add product prices

Compare discounts

Check stock quantities

Calculate profit

Verify customer eligibility

Without calculations and comparisons, the business cannot function.

JavaScript works the same way.

Variables store information.

Operators help JavaScript work with that information.

What Are Operators?

Operators are special symbols that perform operations on values.

Example:

let total = 10 + 5;

Here:

10 → Operand

5 → Operand

+ → Operator

Output:

15

Operators are the tools JavaScript uses to solve problems.


Arithmetic Operators

These operators perform mathematical calculations.

Think of them as the calculator inside JavaScript.


Addition (+)

Example:

let price1 = 100;

let price2 = 200;

let total = price1 + price2;

console.log(total);

Output:

300

Subtraction (-)

Example:

let balance = 1000;

let purchase = 300;

let remaining =
balance - purchase;

console.log(remaining);

Output:

700

Multiplication (*)

Example:

let quantity = 5;

let price = 100;

let total =
quantity * price;

Output:

500

Division (/)

Example:

let totalBill = 1000;

let friends = 4;

let share =
totalBill / friends;

Output:

250

Modulus (%)

Returns the remainder.

Example:

10 % 3

Output:

1

Because:

10 ÷ 3

= 3 remainder 1

Real World Example

Checking even numbers:

let number = 8;

if(number % 2 === 0){

console.log(
"Even Number"
);

}

Output:

Even Number

Increment Operator (++)

Adds 1.

Example:

let visitors = 10;

visitors++;

Result:

11

E-Commerce Example

let cartItems = 2;

cartItems++;

Customer added one more item.

Now:

3

Decrement Operator (--)

Subtracts 1.

Example:

let stock = 50;

stock--;

Output:

49

Assignment Operators

Used for assigning values.


Basic Assignment

let age = 25;

Meaning:

Store 25 inside age

Add and Assign

Instead of:

score = score + 10;

Use:

score += 10;

Cleaner.


Subtract and Assign

balance -= 500;

Equivalent to:

balance =
balance - 500;

Comparison Operators

These operators compare values.

Result:

true

or

false

Equal To (==)

Example:

10 == 10

Output:

true

Dangerous Example

"10" == 10

Output:

true

Why?

Because JavaScript automatically converts values.

This behavior can cause bugs.


Strict Equality (===)

Professional developers prefer:

===

Example:

"10" === 10

Output:

false

Because:

String



Number

This comparison is safer.


Not Equal (!=)

Example:

10 != 20

Output:

true

Strict Not Equal (!==)

Example:

"10" !== 10

Output:

true

Different data types.


Greater Than (>)

Example:

20 > 10

Output:

true

Less Than (<)

Example:

5 < 10

Output:

true

Greater Than or Equal (>=)

Example:

18 >= 18

Output:

true

Less Than or Equal (<=)

Example:

10 <= 20

Output:

true

Real World Example

AQAD Retailer Registration:

let minimumOrder = 100;

let orderValue = 150;

orderValue >= minimumOrder

Output:

true

Order can proceed.


Logical Operators

Logical operators combine conditions.

Think of them as decision-making tools.


AND (&&)

All conditions must be true.

Example:

let age = 25;

let verified = true;

age >= 18 &&
verified === true

Output:

true

Real World Example

User login:

emailValid &&
passwordValid

Both conditions required.


OR (||)

At least one condition must be true.

Example:

true || false

Output:

true

Example

isAdmin ||
isManager

Either role is allowed.


NOT (!)

Reverses value.

Example:

!true

Output:

false

Type Conversion

This is where many beginners become confused.


What Is Type Conversion?

Type conversion means changing one data type into another.

Example:

"100"

String.

Convert to:

100

Number.


Automatic Type Conversion

JavaScript sometimes converts values automatically.

Example:

"5" + 2

Output:

52

Many beginners expect:

7

But JavaScript converts:

2



"2"

Then joins:

"5" + "2"



"52"

Another Example

"10" - 5

Output:

5

Interesting.

This time JavaScript converts:

"10"



10

and performs subtraction.


Why Type Conversion Causes Bugs

Example:

let age = "18";

if(age === 18){

console.log(
"Adult"
);

}

Output:

Nothing

Because:

String



Number

Explicit Type Conversion

Professional developers prefer explicit conversion.


Number()

Convert string to number.

Example:

Number("100")

Output:

100

Form Example

User enters:

"500"

from input field.

Convert:

const amount = Number(inputValue);

Now calculations work correctly.


String()

Convert value to string.

Example:

String(100)

Output:

"100"

Boolean()

Convert value to boolean.

Example:

Boolean(1)

Output:

true

Example:

Boolean(0)

Output: false

Truthy and Falsy Values

JavaScript treats certain values as:

true

or

false

Falsy Values

Examples:

false

0

""

null

undefined

NaN

All behave as:  false


Truthy Values

Examples:

"Hello"

100

[]

{}

true

All behave as:

true

Real World Example

Login Check:

let username = "";

if(username){

console.log(
"User Exists"
);

}

Output:

Nothing

Because empty string is falsy.

Common Beginner Mistakes


Using == Instead of ===

Avoid:

10 == "10"

Prefer:

10 === "10"

Ignoring Data Types

Wrong:

let total =
"100" + 50;

Output:

10050

Unexpected.


Forgetting Number Conversion

Form values arrive as strings.

Always validate.


Best Practices

✅ Prefer:  ===   instead of:  ==

✅ Convert values explicitly.

✅ Validate user input.

✅ Understand truthy and falsy values.


Real Project Example

AQAD Marketplace Checkout:

const quantity = 5;

const price = 10;

const total = quantity * price;

const minimumOrder = 20;

if(total >= minimumOrder){

console.log( "Checkout Allowed" );

}

Output: Checkout Allowed

This is how operators help businesses make decisions automatically.

Key Takeaways

Remember:

✅ Arithmetic operators perform calculations.

✅ Comparison operators compare values.

✅ Logical operators combine conditions.

✅ JavaScript performs automatic type conversion.

✅ Prefer explicit conversion.

✅ Always use strict equality (===).

Post a Comment

0 Comments