How to call a function in Javascript

2
How to call a function in Javascript

Hey friends, Welcome to CodeWithNepal today in this blog you’ll learn how to call a function in Javascript. In the earlier blog, I shared Photomosaic Generator which I recommend you to watch.

Function in Javascript

A function is a block of code that performs a specific task. Parts are written in order to make code more reusable and easier to read. When a function is invoked or called, the code inside the part is executed.

Functions are an essential part of any programming language and JavaScript is no exception. In JavaScript, functions are objects. This means that they can be passed around as arguments to other functions, returned from functions, and assigned to variables.

Functions are declared with the function keyword followed by a name. The function name can be any valid JavaScript identifier.

Call a function in Javascript

example:

Assuming you have a function named myFunction:

To call a function in JavaScript, you need to use the function name followed by parentheses ().

myFunction();

Inside the parentheses, you can specify any arguments that the function needs to take. For example, if the function takes two arguments, you would write them like this:

myFunction(argument1, argument2);

You can also call a function without any arguments:

myFunction();

example:

The call() method is a way to call a function with a given value and arguments provided individually.
This is especially useful for borrowing methods or for calling a superclass method from a subclass.

The call() function can be used on any function, including built-in functions. It can also be used on an object that is a function.

When called, the call() function will call the function with this value set to the first argument and the arguments provided as the second and subsequent arguments.

For example, let’s say we have a simple function that prints out this value and the arguments:

function printArgs(a, b, c) {
console.log(this, a, b, c);
}

If we call this function using the call() method we can set this value and the arguments individually.

You may like this blog:

How to call a function in Javascript.

In JavaScript, all functions are object methods.

If a function is not a method of a JavaScript object, it is a function of the global object (see the previous chapter).

The example below creates an object with 3 properties, firstName, lastName, and fullName.

const person = {
  firstName:"Code",
  lastName: "js",
  fullName: function () {
    return this.firstName + " " + this.lastName;
  }
}

// This will return "Code js":
person.fullName();  

The example above this refers to the person object.

this.firstName means the firstName property of this.

Same as:

this.firstName means the firstName property of a person.

JavaScript call() Method

const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
const person1 = {
  firstName:"Code",
  lastName: "js"
}
const person2 = {
  firstName:"Program",
  lastName: "js"
}

// This will return "Code js":
person.fullName.call(person1);

This example calls the fullName method of person, using it on person2:

const person = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
const person1 = {
  firstName:"Code",
  lastName: "js"
}
const person2 = {
  firstName:"Program",
  lastName: "js"
}

// This will return "Program js"
person.fullName.call(person2);

Call() Method with Arguments

The call() the method can accept arguments:

const person = {
  fullName: function(city, country) {
    return this.firstName + " " + this.lastName + "," + city + "," + country;
  }
}

const person1 = {
  firstName:"Code",
  lastName: "js"
}

person.fullName.call(person1, "Butwal", "Nepal")

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here