Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Understanding JavaScript Functions: A Beginner-Friendly Guide to Clean, Reusable Code

--

Press enter or click to view image in full size
Photo by Claudio Schwarz on Unsplash

One of the best things about JavaScript and programming in general is the ability to write clean, reusable code. And one of the key tools that help us do this is the function.

If you have ever found yourself writing the same lines of code more than once, then it’s time to meet your new best friend: functions. They help you follow the DRY principle Don’t Repeat Yourself which makes your code easier to manage, scale, and debug.

In this article, we will break down what functions are, why they’re useful, and how you can start using them effectively in JavaScript.

What Exactly is a Function?

A function is simply a block of code that performs a specific task. Think of it as a mini program within your main program — a tool you can define once and use over and over again.

Here is the basic structure of a function in JavaScript:

function addTwoNums() {
let a = 10;
let b = 20;
let c = a + b;
console.log(c);
}

addTwoNums(); // Output: 30

🧠 What’s Happening Here?

We declared a function called addTwoNums.

Inside the function, we created two variables, added them, and logged the result.

To run the function, we used addTwoNums(); this is known as calling the function.
Without calling it, the code inside the function won’t execute. Functions just sit there waiting for you to tell them when to act.

⚙️ Making It Reusable with Parameters

While the above function works, it's hard-coded to add 10 and 20. What if we want to add any two numbers?

That is where parameters come in.

function addTwoNums(a, b) {
let c = a + b;
console.log(c);
}

addTwoNums(10, 20); // Output: 30

🤔 Let’s Break That Down:

a and b are parameters — they act like placeholders.

When calling the function, 10 and 20 are arguments the actual values we pass in.

Get Christiana Osedi’s stories in your inbox

Join Medium for free to get updates from this writer.

Now the function is dynamic we can reuse it for any set of numbers:

addTwoNums(5, 15); // Output: 20
addTwoNums(100, 250); // Output: 350

🧱 Why Functions Matter

Using functions isn’t just about writing less code is about writing better code.

Here is why functions are essential:

✅ Avoid repetition (DRY principle)

✅ Keep your code clean and organized

✅ Make it easier to test and debug

✅ Break down complex problems into smaller chunks

✅ Improve readability and collaboration in teams

📝 Final Thoughts

If you’re new to JavaScript, functions might seem like just another feature to memorize but they are so much more. They are the foundation for writing professional, scalable code.

So the next time you catch yourself copying and pasting the same logic, pause and ask: Can I turn this into a function?

Chances are, the answer is yes and your future self will thank you.

🔁 Practice Idea:
Try creating a function that multiplies two numbers. Then tweak it to accept three numbers. Finally, challenge yourself to write a function that returns a message based on age input like:

// "You are eligible to vote" or "Sorry, you must be 18+"

Thank you for being a part of the community

Before you go:

--

--

Christiana Osedi
Christiana Osedi

Written by Christiana Osedi

Frontend Developer | Tech Writer @ Stackademic | Passionate about clean code web accessibility, and simplifying tech for beginners. Sharing practical coding tip

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.