Why Functions Exist

Discover why programmers invented functions and how they save you from writing the same code over and over.

Step 1 of 4

The copy-paste trap

Imagine you're building a website and you need to calculate sales tax in five different places. Without functions, you'd copy and paste the same math five times. Now imagine the tax rate changes — you'd have to find and update every single copy. Miss one? Bug. This is the core problem functions solve: they let you write a piece of logic once and reuse it everywhere. Programmers call this the DRY principle — Don't Repeat Yourself.

Think of it this way: A function is like a recipe card. You write the steps once, give it a name like 'Chocolate Cake', and whenever you want cake, you follow the recipe instead of figuring it out from scratch every time. If you improve the recipe, you only update one card — not fifty sticky notes scattered around the kitchen.
Web Standard
Functions are one of the fundamental building blocks in JavaScript and in virtually every programming language. The ECMAScript specification defines several kinds of functions — declarations, expressions, arrow functions, generators, and async functions. You'll learn the first three in this module.
JAVASCRIPTREAD ONLY
CONSOLE
Click "Run" to execute your code...