Why Functions Exist
Discover why programmers invented functions and how they save you from writing the same code over and over.
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.
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.