Back to JavaScript
2026-01-065 min read

Function Call (JavaScript)

Learn Function Call (JavaScript) step by step with clear examples and exercises.

Why This Matters

Function calls are a fundamental aspect of JavaScript programming that enable you to write reusable and efficient code. By understanding how to effectively use function calls, you can create more organized and manageable scripts. This guide covers the importance of function calls, prerequisites, core concepts, worked examples, common mistakes, practice questions, and frequently asked questions.

Function calls help reduce redundancy, improve modularity, and make your code easier to understand, test, and maintain. They are essential for solving real-world problems such as handling user input, performing complex calculations, and creating interactive web pages. By using functions, you can encapsulate logic, making it reusable across different parts of your application.

Prerequisites

Before diving into the core concept of JavaScript function calls, you should have a basic understanding of:

  1. Variables and data types in JavaScript
  2. Basic operators (arithmetic, comparison, logical)
  3. Control structures (if-else statements, loops)
  4. Basic DOM manipulation
  5. Event handling
  6. Understanding the concept of scope in JavaScript
  7. Familiarity with ES6 features like arrow functions and template literals (optional but recommended)

Core Concept

A function is a block of code that performs a specific task. In JavaScript, functions are defined using the function keyword followed by the function name and parentheses containing any required parameters. To call a function, you use its name followed by parentheses containing any arguments (optional or required).

// Define a simple function
function greet(name) {
console.log(`Hello, ${name}`); // Using template literals for better readability
}

// Call the function with an argument
greet("Alice"); // Output: Hello, Alice

Inside functions, you can use variables and perform calculations just like in regular code. Functions can also contain other functions (nested functions) and return a value using the return keyword.

Function Declaration vs. Expression (expanded)

There are two ways to define functions in JavaScript: declaration and expression. The difference lies in where they are placed in your code and how they are called.

  • Function Declaration: This is the traditional way of defining a function, and it is hoisted to the top of its containing scope.
function greet(name) {
console.log(`Hello, ${name}`); // Using template literals for better readability
}
  • Function Expression (Anonymous Function): This type of function is assigned to a variable or object property and can be used as an argument to other functions.
const greet = function(name) {
console.log(`Hello, ${name}`); // Using template literals for better readability
};

Worked Example

Let's create a function that calculates the factorial of a number using both function declaration and expression:

// Function Declaration
function factorialDecl(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorialDecl(n - 1);
}
}

// Function Expression
const factorialExp = function factorialExp(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorialExp(n - 1);
}
};

// Test the functions with some examples
console.log(factorialDecl(5)); // Output: 120
console.log(factorialExp(7)); // Output: 5040

In this example, we've used both function declaration and expression to calculate the factorial of a number recursively. The difference between the two is that function declarations are hoisted, while function expressions are not.

Common Mistakes

  1. Forgetting to return a value: If a function doesn't return a value, it will implicitly return undefined. Make sure to use the return keyword when needed.
  1. Not passing arguments correctly: Be mindful of how you pass arguments to functions, and make sure they match the expected data types and formats.
  1. Ignoring hoisting: In JavaScript, variable declarations are hoisted to the top of their containing scope, but function declarations are not. This can lead to unexpected behavior if you don't account for it.
  1. Not handling edge cases: Make sure your functions work correctly for all possible input values, including special cases like null, undefined, and NaN.
  1. Overusing anonymous functions: While anonymous functions (function expressions) can be useful in certain situations, overuse of them can make your code harder to read and debug. Use named functions whenever possible for better readability and maintainability.

Function Hoisting (expanded)

Function declarations are hoisted to the top of their containing scope, while function expressions are not. This means that you can call a function declaration before it is defined without causing an error, but you cannot do the same with a function expression.

// Function Declaration
myFunction(); // Output: undefined
function myFunction() {
console.log("Hello!");
}

// Function Expression (not hoisted)
myFunctionExp(); // Error: myFunctionExp is not defined
let myFunctionExp = function() {
console.log("Hello!");
};

Practice Questions

  1. Write a function that finds the maximum number in an array using both function declaration and expression.
  2. Create a function that calculates the sum of the squares of the first n natural numbers using both function declaration and expression.
  3. Implement a function that generates Fibonacci sequence up to the nth term using both function declaration and expression.
  4. Write a function that reverses a given string using both function declaration and expression.
  5. Create a function that finds all prime numbers up to a given number using both function declaration and expression.
  6. Implement a function that calculates the factorial of a number using recursion with both named and anonymous functions.
  7. Write a function that generates and returns a random password containing uppercase letters, lowercase letters, numbers, and special characters using both function declaration and expression.

FAQ

How do I pass multiple arguments to a JavaScript function?

You can pass multiple arguments to a JavaScript function by separating them with commas within the parentheses.

function greet(name, message) {
console.log(`${message}, ${name}`); // Using template literals for better readability
}

greet("Alice", "Hello"); // Output: Hello, Alice

Can I use variables as function names in JavaScript?

Yes, you can dynamically create and call functions using variables as their names. However, be cautious when doing this, as it can lead to hard-to-debug issues if not used correctly.

let funcName = "greet";
let greetingMessage = "Hello, World!";

window[funcName] = function() {
console.log(greetingMessage);
};

// Call the dynamically created function
greet(); // Output: Hello, World!
Function Call (JavaScript) | JavaScript | XQA Learn