Functions - Level I
A function is a named, reusable block of code that performs one task. You define it once and run it whenever you need that task done, optionally giving it input values and getting a result back. This first level of functions covers the essentials every JavaScript program relies on: how to create functions, call them, pass data in, and get data out.
Why programs are built from functions
Without functions, the same lines of code get copied wherever a task repeats, for example calculating tax on every product. When the tax rule changes, you have to find and fix every copy. With a function, you fix one place.
Functions also give a name to an idea. A line like calculateTax(price) tells the reader what happens without forcing them to read the arithmetic. Breaking a big problem into small, well-named functions is one of the most valuable habits a programmer can build.
Anatomy of a function
// declaration: name, parameters, body
function calculateTax(amount, rate = 0.18) {
const tax = amount * rate;
return Math.round(tax * 100) / 100;
}
// calling it with arguments
const gst = calculateTax(1200); // 216
const reduced = calculateTax(1200, 0.05); // 60
console.log(`Total: ${1200 + gst}`);
- Name:
calculateTax. Use a verb that says what the function does. - Parameters:
amountandrate, placeholders listed in the brackets.rate = 0.18gives a default value when no argument is supplied. - Body: the statements inside the curly braces.
- Return value: whatever follows
returnis sent back to the caller. A function with noreturngives backundefined.
Ways to create a function
| Form | Example | Notes |
|---|---|---|
| Declaration | function greet(n) { ... } | Hoisted, so it can be called before the line where it appears. |
| Expression | const greet = function (n) { ... }; | Usable only after the assignment runs. |
| Arrow function | const greet = (n) => `Hi ${n}`; | Short syntax; a single expression is returned automatically. Has no own this. |
All three create the same kind of value. Choosing between them is mostly about style and about how this should behave, which matters more once you work with objects.
Defining is not running
The most common beginner confusion is writing a function and wondering why nothing happens. A definition only describes the task; the code inside runs when you call the function by writing its name followed by brackets, like calculateTax(500).
The opposite slip is just as common: writing calculateTax without brackets. That refers to the function itself rather than running it, so console.log(calculateTax) prints the function's code instead of a number. Both behaviours are useful in the right place, as you will see with callbacks and event handlers.
Good function habits from the start
- Keep each function focused on one job. If you need "and" to describe it, consider splitting it.
- Prefer returning a value over printing inside the function. A function that returns can be reused anywhere; one that only logs cannot.
- Pass in what the function needs through parameters instead of reading outside variables, so its behaviour is predictable.
What this level covers next
The lessons that follow go deeper into each piece introduced here: the function definition itself, how values are passed to parameters, how return values work, how local and global variables differ, and how functions behave as objects in JavaScript.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.