Calling Functions


Invoking or calling a function in JavaScript executes the code inside its block. To call a function, you simply need to specify the function's name followed by parentheses. This action passes control to the function, and once the function has finished executing, control returns to the point where it was called.


Example 1: Basic Function Call

// Defines a function named greet
function greet() {
  console.log("Hello, world!");
}

// Calls the greet function
greet();

Explanation:

The greet function is defined to log a message. The line greet(); invokes the function, causing "Hello, world!" to be displayed in the console.


Example 2: Function Call with Arguments

// Defines a function that accepts one argument
function greetUser(username) {
  console.log(`Hello, ${username}!`);
}

// Calls the function with the argument "Alice"
greetUser("Alice");

Explanation:

The greetUser function takes a username as a parameter. When we call greetUser("Alice"), the value "Alice" is passed into the function, resulting in "Hello, Alice!" being logged.


Example 3: Function Call with a Return Value

// Defines a function that calculates the sum of two numbers
function add(a, b) {
  return a + b;
}

// Calls the function and stores the returned value
let sum = add(5, 3);
console.log(sum); // Output: 8

Explanation:

The add function returns the sum of its parameters. We call the function with add(5, 3) and assign the returned value of 8 to the sum variable.


Example 4: Calling a Function Expression

// Defines a function expression and assigns it to a variable
const multiply = function(x, y) {
  return x * y;
};

// Calls the function through the variable
let product = multiply(4, 5);
console.log(product); // Output: 20

Explanation:

A function is defined as an expression and assigned to the multiply constant. We then call this function using the variable name multiply(4, 5), and the result is stored.


Example 5: Immediately Invoked Function Expression (IIFE)

// Defines and immediately calls a function
(function() {
  console.log("This function is called immediately.");
})();

Explanation:

This is an Immediately Invoked Function Expression (IIFE). The function is defined within parentheses and then immediately called by the trailing parentheses (), executing the code inside it once.


Example 6: Calling a Method on an Object

// Defines an object with a method
const person = {
  name: "John",
  sayHello: function() {
    console.log(`Hello from ${this.name}`);
  }
};

// Calls the method on the person object
person.sayHello(); // Output: Hello from John

Explanation:

The sayHello function is a method of the person object. We call this method using dot notation (person.sayHello()), which executes the function in the context of the object.


Example 7: Using call() to Invoke a Function

// Defines a simple function
function introduce(language) {
  console.log(`I am learning ${language} with ${this.name}.`);
}

const student = { name: "Chris" };

// Calls the introduce function with a specified 'this' context
introduce.call(student, "JavaScript"); // Output: I am learning JavaScript with Chris.

Explanation:

The call() method invokes the introduce function, allowing us to explicitly specify the value of this inside the function. Here, this is set to the student object, and "JavaScript" is passed as an argument.