Search⌘ K
AI Features

User-Defined Functions

Explore how to define and call user-defined functions in JavaScript to organize decision-making logic efficiently. Learn to build reusable functions that handle multiple inputs and outputs, and integrate them into interactive web pages. Understand how to scale your smart thermostat project with clear, maintainable code and optimize it for real-world scenarios like energy efficiency.

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 ... ...