A JavaScript function call is an action that invokes or executes a defined function to perform a specific task or computation. Functions in JavaScript are reusable blocks of code that can be defined with a name and a set of parameters. When you call a function, you are instructing the JavaScript interpreter to execute the code inside that function. In this article, we are going to learn JavaScript Function Call.
Syntax:
call()
Return Value:
It calls and returns a method with the owner object being the argument.
Example 1: Here is a basic example to describe the use of the call() method.
Javascript
function product(a, b) {
return a * b;
}
let result = product.call(this, 20, 5);
console.log(result);
|
Example 2: This example describes the use of function calls with arguments.
Javascript
let employee = {
details: function (designation, experience) {
return this.name
+ " "
+ this.id
+ designation
+ experience;
}
}
let emp1 = {
name: "A",
id: "123",
}
let emp2 = {
name: "B",
id: "456",
}
let x = employee.details.call(emp2, " Manager ", "4 years");
console.log(x);
|
OutputB 456 Manager 4 years
Example 3: This example describes binding a function to an object.
Javascript
let obj = { a: 12, b: 13 };
function sum() {
return this.a + this.b;
}
console.log(sum.call(obj));
|
We have a complete list of Javascript Functions, to check those please go through the Javascript Function Complete reference article.
Supported Browser:
- Google Chrome 1.0
- Firefox 1.0
- Microsoft Edge 12.0
- Internet Explorer 5.5
- Opera 4.0
- Safari 1.0
Last Updated :
08 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...