...

/

Else-If Chains and Nesting

Else-If Chains and Nesting

Learn JavaScript else-if and range checks by building a smart home weather response system that balances comfort and energy bills.

The project

Think of a smart house with sensors that track temperature. A basic system might only choose between two states: turn on the heater or do nothing. But that’s limited. In reality, we often feel cold, comfortable, or hot. A smarter system should adjust itself based on all of these states. In programming, we handle this with multi-branch conditionals.

For example, the heater should be turned on when the house is too cold. When the temperature is just right, no action is needed. When it’s a bit warm, a fan should be turned on. And when it’s really hot, the AC should kick in. This is what makes a smart home feel more practical and human-centered.

The programming concept

We already know if and else conditional statements. Here’s another example of decision-making in code: setting ticket prices.

  • If age < 12 → child ticket

  • If age is 12–17 → teen ticket

  • If age ≥ 18 → adult ticket

In plain terms, that’s an if → else if → else chain.

In JavaScript, ...