As one of the most popular programming languages, JavaScript is used extensively in web development, mobile applications, and game development. One of the core features of JavaScript is its ability to use functions. In this article, we’ll be discussing JavaScript function invocation, which is an essential concept for all JavaScript developers.
What is a JavaScript Function?
Before we dive into function invocation, let’s first define what a function is in JavaScript. A function is a set of instructions that performs a specific task. It’s a block of code that can be reused whenever needed. Functions can take in inputs (arguments) and produce outputs (return values). Here’s an example of a basic JavaScript function:
function addNumbers(a, b) {
return a + b;
}
In this example, the addNumbers
function takes in two arguments (a
and b
) and returns their sum.
Function Invocation
Function invocation is the process of calling a function. When a function is invoked, its code is executed, and any arguments passed to the function are evaluated. In JavaScript, there are several ways to invoke a function:
1. Function Call
The most common way to invoke a function in JavaScript is by using the function call syntax. This involves calling the function by its name, followed by parentheses, and any arguments that need to be passed to the function. Here’s an example:
addNumbers(2, 3); // Returns 5
In this example, the addNumbers
function is invoked with the arguments 2
and 3
. The function returns the value 5
.
2. Method Call
A method is a function that is a property of an object. In JavaScript, methods can be invoked using the dot notation. Here’s an example:
var myObj = {
name: "John",
sayHello: function() {
console.log("Hello, " + this.name);
}
};
myObj.sayHello(); // Outputs "Hello, John"
In this example, the sayHello
method of the myObj
object is invoked. The method outputs “Hello, John” to the console.
3. Constructor Invocation
In JavaScript, functions can also be used as constructors to create objects. To create a new object using a constructor function, you use the new
keyword followed by the function name and any arguments that need to be passed to the constructor. Here’s an example:
function Person(name, age) {
this.name = name;
this.age = age;
}
var person1 = new Person("John", 30);
console.log(person1.name); // Outputs "John"
console.log(person1.age); // Outputs 30
In this example, the Person
function is used as a constructor to create a new Person
object. The name
and age
properties of the object are set to “John” and 30
, respectively.
4. Apply and Call
JavaScript provides two methods, apply()
and call()
, that can be used to invoke functions with a specified this
value and arguments. These methods are useful when you want to call a function with a specific this
value or pass an array of arguments to the function.
Here’s an example using apply()
:
function greet() {
console.log("Hello, " + this.name);
}
var person = { name: "John" };
greet.apply(person); // Outputs "Hello, John"

In this example, the greet
function is invoked using the apply()
method with the person
object as the this
value.
And here’s an example using call()
:
function greet(greeting) {
console.log(greeting + ", " + this.name);
}
var person = { name: "John" };
greet.call(person, "Hi"); // Outputs "Hi, John"

In this example, the greet
function is invoked using the call()
method with the person
object as the this
value and the argument "Hi"
.
The Global Object
In JavaScript, there is always a global object, which is the top-level object in the runtime environment. In web browsers, the global object is the window
object, and in Node.js, it’s the global
object. When a function is invoked without a specified this
value, it defaults to the global object. Here’s an example:
function greet() {
console.log("Hello, " + this.name);
}
var name = "John";
greet(); // Outputs "Hello, John"
In this example, the greet
function is invoked without a specified this
value. Since there is no name
property in the function’s scope, it defaults to the global name
variable, which is set to “John”.
Binding this with bind()
Sometimes, you may want to bind a function to a specific this
value. In JavaScript, you can do this using the bind()
method. The bind()
method returns a new function with the this
value set to the object that you pass as an argument. Here’s an example:
var person = {
name: "John",
sayHello: function() {
console.log("Hello, " + this.name);
}
};
var sayHelloToMary = person.sayHello.bind({ name: "Mary" });
sayHelloToMary(); // Outputs "Hello, Mary"
In this example, the sayHello
method of the person
object is bound to a new object with the name
property set to “Mary”. The sayHelloToMary
function is then invoked, and it outputs “Hello, Mary”.
Conclusion
In this article, we’ve covered the basics of JavaScript function invocation. We’ve discussed the different ways to invoke a function in JavaScript, including function calls, method calls, constructor invocation, and apply and call methods. We’ve also talked about the global object and how it affects function invocation, as well as how to bind this
with the bind()
method.
Understanding JavaScript function invocation is essential for writing effective and efficient JavaScript code. By mastering this concept, you’ll be able to write clean, modular, and reusable code that can be easily maintained and scaled. Keep practicing and experimenting with different ways to invoke functions, and you’ll become a proficient JavaScript developer in no time.
📕 Related articles about Javascript
- JavaScript Asynchronous: Understanding and Implementing It Efficiently
- JavaScript Comparisons: A Comprehensive Guide
- JavaScript Function Definitions
- How to Use Javascript for Web Animations
- JavaScript Type Conversion: Understanding the Ins and Outs
- How to Learn Javascript Advanced Techniques