Function Types and Function Calls in Javascript | AsgarTech
Function and Types of Function in Js | AsgarTech
Functions in JavaScript are a set of statements that perform a specific task or return a value. A function can be defined using the function keyword followed by the function name, a list of parameters (optional), and a block of code that defines the function's behavior.
Here's an example of a function that takes two parameters and returns their sum:
function sum(a, b) {
return a + b;
}
To call a function in JavaScript, you need to use its name followed by parentheses containing any arguments passed to the function.
Here's an example of calling the sum function and passing two arguments:
let result = sum(2, 3); // calling the sum function with arguments 2 and 3
console.log(result); // Output: 5
Functions can also be assigned to variables or stored in arrays and objects as values. Here's an example:
// assigning a function to a variable
let multiply = function(a, b) {
return a * b;
}
// calling the multiply function with arguments 3 and 4
let result = multiply(3, 4);
console.log(result); // Output: 12
// storing a function in an object
let obj = {
sayHello: function() {
console.log('Hello!');
}
};
// calling the sayHello function in the object
obj.sayHello(); // Output: "Hello!"
In addition to these basic concepts, functions in JavaScript can also be nested, invoked recursively, passed as arguments to other functions, and have default parameter values and variable-length argument lists. These features make functions a powerful tool for building complex and reusable code in JavaScript.
Types of Function in javascript
Named function: A named function is a function that has a name defined using the function keyword followed by the function name.
function add(a, b) {
return a + b;
}
Anonymous function: An anonymous function is a function that has no name and is defined using the function keyword followed by the function parameters and body.
let add = function(a, b) {
return a + b;
}
Arrow function: An arrow function is a shorter syntax for writing anonymous functions. It is defined using the => operator and can be used to define functions that take one or more parameters and return a value.
let add = (a, b) => {
return a + b;
}
Method: A method is a function that is defined as a property of an object.
let obj = {
name: 'John',
greet: function() {
console.log(`Hello, my name is ${this.name}`);
}
};
obj.greet(); // Output: "Hello, my name is John"
Constructor function: A constructor function is a special type of function that is used to create objects. It is defined using the function keyword and is called using the new keyword.
function Person(name, age) {
this.name = name;
this.age = age;
}
let john = new Person('John', 30);
console.log(john.name); // Output: "John"
console.log(john.age); // Output: 30
Callback function: A callback function is a function that is passed as an argument to another function and is executed when the first function has completed its task.
function fetchData(url, callback) {
// fetch data from the specified url
let data = fetch(url);
// execute the callback function with the fetched data
callback(data);
}
function displayData(data) {
console.log(data);
}
fetchData('https://example.com', displayData);
These are the most common types of functions in JavaScript. Understanding these types and how to use them can help you write more efficient and reusable code.
No comments: