
If you’re learning JavaScript, one of the first (and most important) things you’ll need to understand is how to call a function.
Functions are like reusable blocks of code — you write them once and use them as many times as you want.
In this guide, we’ll walk through everything you need to know about calling a function in JavaScript, step by step, with examples and best practices.
What Is a Function in JavaScript?
A function in JavaScript is a set of statements that performs a task or calculates a value.
You can think of it like a mini-program that lives inside your code.
Here’s a basic example:
function greet() {
console.log("Hello, world!");
}
Code language: JavaScript (javascript)
In this example:
functionis the keyword that tells JavaScript you’re defining a function.greetis the name of the function.- The code inside
{}is what runs when you call the function.
How to Call (or Invoke) a Function in JavaScript
Once you’ve defined a function, you can call it (or invoke it) by using its name followed by parentheses ().
Example:
greet(); // Output: Hello, world!Code language: JavaScript (javascript)
Explanation:
- The parentheses tell JavaScript to execute the code inside the function.
- If you just write
greetwithout(), it refers to the function itself, not its result.
Calling a Function with Parameters
Functions can also take parameters — values you pass in to customize what the function does.
Example:
function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("John"); // Output: Hello!
Code language: JavaScript (javascript)
Here:
nameis a parameter.- When we call
greetUser("John"),"John"is the argument that gets passed in.
Calling a Function That Returns a Value
Sometimes, you’ll want a function to return a result that you can use later.
Example:
function add(a, b) {
return a + b;
}
let sum = add(10, 5);
console.log(sum); // Output: 15
Code language: JavaScript (javascript)
In this example:
- The
returnstatement sends the result back to where the function was called. - You can store it in a variable (
sum) or use it directly.
Different Ways to Call Functions
There are a few ways to call a function in JavaScript:
- Direct Call
greet(); - Call Inside Another Function
function welcome() { greet(); } welcome(); // Output: Hello, world! - Call from an Event
<button onclick="greet()">Click Me</button> - Call Using
call()orapply()function showName() { console.log(this.name); } const user = { name: "John" }; showName.call(user); // Output: John
Common Mistakes When Calling Functions
🚫 Forgetting the parentheses
greet; // ❌ does not call the functionCode language: JavaScript (javascript)
✅ Correct
greet(); // ✅ calls the functionCode language: JavaScript (javascript)
🚫 Using undefined parameters
If your function expects arguments, make sure to pass them when you call it.
Best Practices
✅ Use meaningful function names
✅ Keep functions small and focused
✅ Always use return if your function produces a value
✅ Comment your code if the logic is complex
💡 Quick Recap
| Concept | Example | Description |
|---|---|---|
| Define a function | function greet() { ... } | Creates a function |
| Call a function | greet(); | Executes the function |
| Function with parameters | function greetUser(name) { ... } | Takes input values |
| Function with return value | return a + b; | Sends back a result |
Final Thoughts
Calling a function in JavaScript is simple — just add () after its name.
But mastering when and how to call functions effectively is what makes your code clean, modular, and powerful.
As you practice, you’ll find yourself creating small functions to handle everything from user interactions to API calls — making your code easier to manage and debug.
Master JavaScript the Smart Way — Build Real Projects, Not Just Syntax!
In my JavaScript Essentials Course, you’ll learn how to think like a developer — through hands-on lessons, real-world projects, and clear, step-by-step guidance designed for beginners.
Discover more from Prime Inspire
Subscribe to get the latest posts sent to your email.



