Function Calling Other Function In JavaScript

Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Function Calling Other Function In JavaScript.

In JavaScript, one function can call another function, allowing for the creation of modular and reusable code. This is known as function composition and it helps to break down complex logic into smaller and more manageable chunks.

Function Calling Other Function In JavaScript

A function can call another function by using the function name followed by a set of parentheses. For example, let’s say we have a function called “greet” that logs “Hello, World!” to the console and another function called “sayHi” that calls the greet function:

Code For Function Calling Other Function In JavaScript

function greet() {
    console.log("Hello, World!");
}

function sayHi() {
    greet();
}

sayHi();

Output

Hello, World!

Functions can also pass data to other functions by passing in arguments. For example, let’s say we have a function called “add” that takes two parameters, “a” and “b”, and returns their sum, and another function called “calculate” that calls the add function and passes in two numbers:

Function Calling Other Function With Parameter

function add(a, b) {
    return a + b;
}

function calculate() {
    let result = add(2, 3);
    console.log(result);
}
calculate();

Output

5

A function can also return another function, this is known as higher-order functions. This allows you to create more flexible and dynamic code. For example, let’s say we have a function called “createGreeting” that takes a name as an argument and returns a function that greets that person:

function createGreeting(name) {
    return function() {
        console.log(`Hello, ${name}!`);
    }
}

let greetJohn = createGreeting("John");
greetJohn();

Output

Hello, John!

Functions can also be passed as arguments to other functions, this is known as callbacks. For example, let’s say we have an array of numbers and a function called “forEach” that iterates over the array and calls a callback function for each item:

let numbers = [1, 2, 3, 4, 5];
function forEach(array, callback) {
    for (let i = 0; i < array.length; i++) {
        callback(array[i]);
    }
}

forEach(numbers, function(number) {
    console.log(number);
});

Functions can also be passed as arguments to other functions, returned by other functions and be used to create higher-order functions, making JavaScript a more flexible and dynamic language.

Conclusion

In conclusion, JavaScript functions can call other functions, passing data back and forth and creating more modular and reusable code. This is known as function composition and it helps to break down complex logic into smaller and more manageable chunks.

If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment