Writing Custom Functions: A Guide to JavaScript Code Reuse
Stop repeating yourself. Learn the core syntax for writing custom functions in JavaScript to build cleaner, more modular code for your web projects.

Previously in this course, we explored introduction-to-loops-automate-iteration-in-javascript to handle repetitive tasks. While loops are excellent for iterating over data, they aren't always the right tool for grouping logic you need to run at different times throughout your application. In this lesson, we introduce functions, the essential building blocks for code reuse and organization.
Understanding Functions from First Principles
In programming, a function is a named, reusable block of code designed to perform a specific task. Think of a function like a recipe: you write the steps once, give the recipe a name (e.g., "Make Coffee"), and then you can "call" (use) that recipe whenever you want a cup of coffee without having to rewrite the instructions from scratch.
Without functions, you would find yourself copying and pasting the same blocks of logic everywhere in your scripts. This makes your code hard to maintain—if you need to change a calculation, you’d have to hunt down every instance of that code and update it manually. Functions solve this by centralizing your logic.
The Basic Function Syntax
To create a function in JavaScript, we use the function keyword followed by a name, a set of parentheses (), and a pair of curly braces {} that contain the code to be executed.
JAVASCRIPTfunction sayHello() { console.log("Hello, user!"); }
Breaking down the syntax:
function: The keyword that tells JavaScript you are defining a block of code.sayHello: The name of your function. Use descriptive names likecalculateTotalorrenderTaskrather than generic names likedoStuff.(): The parentheses are where we eventually define inputs (parameters), which we will cover in the next lesson. Even when empty, they are required.{ ... }: The body of the function contains the logic. Anything inside these braces runs only when the function is called.
Calling Your Function
Defining a function doesn't make it run immediately. It simply stores the logic in memory for later use. To actually execute the code inside, you must call the function by typing its name followed by parentheses.
JAVASCRIPT// Defining the function function showWelcomeMessage() { console.log("Welcome to your dashboard!"); } // Calling the function showWelcomeMessage(); showWelcomeMessage();
In the example above, calling showWelcomeMessage() twice will print the message to the console twice. This is the essence of code reuse: you define the logic once and invoke it whenever necessary.
Hands-on Exercise: Modularizing Your Dashboard
In our ongoing project, we need a way to greet the user when they open the dashboard. Let's create a function to handle this.
- Create a new file called
functions.js(or add to your existing script). - Write a function named
initializeDashboardthat logs a message like "Dashboard is ready to use!" to the console. - Call the function below the definition.
- Open your browser console to verify the message appears.
JAVASCRIPTfunction initializeDashboard() { console.log("Dashboard is ready to use!"); } initializeDashboard();
Common Pitfalls to Avoid
Even experienced developers run into these minor traps when starting out with functions:
- Forgetting to call the function: It’s common to write a perfect function but forget to actually invoke it. If your code isn't running, ensure you have the
name()syntax at the end of your script. - Case Sensitivity: JavaScript function names are case-sensitive. If you define
renderList(), callingrenderlist()will result in an error. - Defining functions inside loops: While possible, it's generally best practice to define your functions in the global scope or within a module, rather than inside a loop, to avoid redefining the function every single iteration.
Frequently Asked Questions
Q: Can I call a function before I define it? A: In JavaScript, function declarations are "hoisted," meaning they are available to be called even if the definition appears later in the file. However, for readability and best practice, it is recommended to define functions before you call them.
Q: Is there a difference between declaring and calling a function? A: Yes. Declaring creates the instructions (the recipe), while calling executes those instructions (the act of cooking).
Q: Do I need to store functions in a variable? A: You can, but we are using "Function Declarations" here, which are the standard way to define named functions in JavaScript.
Recap
Functions allow you to wrap logic into reusable blocks, significantly improving your ability to manage complex projects like our to-do dashboard. By using the function name() { ... } syntax and invoking them with name(), you avoid code duplication and keep your scripts organized. This approach mirrors modular patterns found in other languages, similar to how defining custom functions in Python or modular programming in PHP helps developers maintain clean, scalable codebases.
Up next: We will learn how to make our functions more dynamic by passing data into them using Parameters and Arguments.
Work with me

Custom WordPress Theme Development
A custom WordPress theme built exactly to your design — fast, clean, and easy to manage. No bloated page builders, no compromises.

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.


