JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



Function Call

JavaScript is a popular programming language used in web development. It is a versatile language that allows developers to create dynamic and interactive web pages. One of the key features of JavaScript is its ability to use functions. Functions are blocks of code that can be called and executed multiple times. In this article, we will discuss function calls in JavaScript.

What is a Function Call?

A function call is a way to execute a function in JavaScript. When a function is called, the code inside the function is executed. The function can take parameters as input and return a value as output. Function calls are used to perform a specific task or to reuse code.

Here is an example of a function call:


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

greet("John");

In this example, we define a function called "greet" that takes a parameter called "name". The function logs a message to the console that includes the name parameter. We then call the function with the argument "John". The output of this code will be "Hello, John!".

How to Call a Function

There are two ways to call a function in JavaScript: function invocation and method invocation.

Function Invocation

Function invocation is the most common way to call a function in JavaScript. It involves calling the function by its name and passing any necessary arguments in parentheses. Here is an example:


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

var result = add(2, 3);
console.log(result);

In this example, we define a function called "add" that takes two parameters, "a" and "b". The function returns the sum of the two parameters. We then call the function with the arguments 2 and 3 and assign the result to a variable called "result". We log the value of "result" to the console, which will be 5.

Method Invocation

Method invocation is a way to call a function that is a property of an object. It involves calling the function using dot notation on the object. Here is an example:


var person = {
  name: "John",
  age: 30,
  greet: function() {
    console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
  }
};

person.greet();

In this example, we define an object called "person" that has two properties, "name" and "age", and a method called "greet". The "greet" method logs a message to the console that includes the "name" and "age" properties of the object. We then call the "greet" method using dot notation on the "person" object. The output of this code will be "Hello, my name is John and I am 30 years old."

Conclusion

Function calls are an essential part of JavaScript programming. They allow developers to create reusable code and perform specific tasks. By understanding how to call functions in JavaScript, developers can create more efficient and effective code.

References

Activity