...

/

User-Defined Functions

User-Defined Functions

Learn JavaScript functions by building a smart home thermostat system to make comfort decisions for multiple rooms and people.

The project

In the last lesson, you built a smart thermostat that decided whether to turn on the fan, heater, or AC based on the temperature. Right now, that code only handles one room.

The next step is scaling it: checking comfort levels in multiple rooms, like the living room, bedroom, and kitchen, each with different temperatures. You could extend it further so the same room adjusts its advice depending on who is there.

Repeating the same decision logic in multiple places makes your code messy, error-prone, and hard to maintain. JavaScript solves this with functions—reusable blocks of code that let you define logic once and use it wherever you need.

The programming concept

You’ve already used built-in functions like console.log() and prompt(). These are just predefined pieces of code that someone else wrote for you.

But what if you need a function that doesn’t exist yet? After all, no programming language can include every function anyone might need, already packaged and ready to go. For instance, do you think there would ever be a built-in function called makeMePizzaPlease("thank you")?

Let’s define our ...