Foundations of Callbacks and Arrays
Functions as Tools
Functions as Tools
Imagine you have a task you need to do over and over again, like greeting someone by name. In programming, repeating code is inefficient. Instead, we can write the instructions once and save them for later use. This saved set of instructions is called a function.
A function is a reusable block of code that performs a specific task.
Think of it like a recipe. You write down the steps to bake a cake once. Then, anytime you want a cake, you just follow the recipe. You don't have to re-invent the process every time. Let's write a simple recipe, or function, in JavaScript.
function greet() {
console.log("Hello there!");
}
Let's break that down:
functionis a special keyword that tells JavaScript, "Hey, I'm about to define a recipe!"greetis the name we've given our function. We can name it almost anything we want.()The parentheses are required. Later, we'll see how they can be used to give our function some ingredients, or data, to work with.{}The curly braces contain the actual instructions, or the steps of our recipe.
Using Your Function
Just defining a function doesn't do anything. We've written the recipe, but we haven't baked the cake yet. To run the code inside a function, you have to call it. You do this by writing the function's name followed by parentheses.
// This calls, or executes, the function
greet();
// Output in the console:
// Hello there!
This is a critical distinction. Writing greet() executes the function. Writing just greet (without the parentheses) refers to the function itself. It's the difference between following the recipe and just holding the recipe card in your hand.
Let's see what happens if we ask the console to show us the function itself, instead of running it.
// This refers to the function, but doesn't run it.
console.log(greet);
// Output in the console:
// function greet() { console.log("Hello there!"); }
Functions Are Just Data
In JavaScript, functions are special. They can be treated just like any other piece of data, such as a number or a piece of text. You can store a function in a , just like you'd store your age or name.
Going back to our recipe analogy, this is like photocopying the recipe card and giving it a new name. The instructions are the same. Look at this:
// Store the greet function in a new variable
let sayHello = greet;
// Now we can call the new variable like a function!
sayHello();
// Output in the console:
// Hello there!
We didn't create a new function. We just created a new name, sayHello, that points to our original greet function. This flexibility is a cornerstone of JavaScript. Because functions can be treated like data, we can pass them around our program. We can hand a 'recipe card' from one part of our code to another, allowing for powerful and dynamic patterns. This is a key idea we'll build on soon.
What is the primary purpose of a function in programming?
Which keyword must be used to start the definition of a function in JavaScript?