Search⌘ K
AI Features

Making Decisions

Explore how to use if-else statements and comparison operators in JavaScript to make decisions based on user input. Learn to build dynamic programs that respond to different conditions, such as creating a smart thermostat that adjusts modes and provides energy-saving tips. Develop skills in writing adaptable code and generating interactive web pages with AI assistance.

The project

Imagine your thermostat at home. Right now, it only shows the temperature: for example, the temperature is 25. That’s not very helpful on its own. What you’d really want is a system that can decide whether to turn on the heater or the air conditioner based on the temperature and your comfort settings. In coding, we use conditionals for this kind of decision-making. Let’s see how that works in JavaScript.

With confidence in your JavaScript skills, you aim to create this thermostat logic yourself! This smart home system should compare the current room temperature with the users’ preferred comfort temperature and decide what to do.

The programming concept

Let’s write a simple program that checks if someone can vote.

  • If a person is 18 or older → print You can vote!.

  • If they’re younger than 18 → print Sorry, too young.

In plain terms: if age ≥ 18 → yes; otherwise → no.

JavaScript can do exactly that with ...