0
0
Javascriptprogramming~3 mins

Why If statement in Javascript? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your program could think and decide like you do in everyday life?

The Scenario

Imagine you are sorting mail by hand, deciding which letters go to which person based on their address. You have to check each letter carefully and place it in the right pile.

The Problem

Doing this by hand is slow and easy to mess up. You might put a letter in the wrong pile or forget to check some letters. It takes a lot of time and focus.

The Solution

The if statement in programming helps you make decisions automatically. It checks conditions and runs the right code, just like sorting letters quickly and correctly without mistakes.

Before vs After
Before
let age = 20;
if (age >= 18) {
  console.log('Adult');
} else {
  console.log('Minor');
}
After
let age = 20;
console.log(age >= 18 ? 'Adult' : 'Minor');
What It Enables

It lets your program choose different paths and actions based on real-time information, making it smart and flexible.

Real Life Example

Think of a traffic light system that changes colors based on time and traffic. The if statement helps decide when to switch lights to keep cars moving safely.

Key Takeaways

If statements help programs make choices.

They save time and reduce mistakes compared to manual checking.

They make your code smart and responsive to different situations.