What is Function in JavaScript


Functions are one of the fundamental building blocks in JavaScript. A function is a reusable block of code that performs a specific task. Functions allow us to organize our code, make it more readable, and avoid repetition of the same code again and again.


Definition of Function in JavaScript

In JavaScript, a function is defined using the function keyword, followed by:

  • Function Name (optional): Function name is the name which is used to call the function.
  • Parameters (optional): The values that the function uses to perform its task.
  • Function Body: A block of code enclosed within curly braces {} that is executed when the function is invoked.


JavaScript Function Example:

function myFunction(parameters) {
    // Function body
    // Code to be executed
}

In the given array example:

  • The function is the keyword that is used to declare a function in JavaScript. In this case, the name of the function is myFunction.

Types of Functions

1. Named Function

A named function has a specific name and can be called by that name to execute the block of code.

function greeting(author) {
    return `Hello, ${author}!`;
}

console.log(greenting("Reena"));
// Output: Hello, Reena!

In the example above, the greeting is a named function that takes one parameter author. And then it returns a greeting message when called. 


2. Anonymous Function

An anonymous function is a function without a name. It is usually assigned to a variable, making it callable through that variable.

const addNumbers = function(a, b) {
    return a + b;
};

console.log(addNumbers(5, 10));
// Output: 15

Here, the function is assigned to the variable addNumbers. Although the function has no name, it can be executed using addNumbers and passing values in this.


3. Arrow Functions (ES6)

Arrow functions provide a short syntax for writing functions in JavaScript. Arrow functions were introduced in ES6 (ECMAScript 2015). They are especially useful for concise one-liners and simplifying function expressions.

Syntax:

const functionName = (parameters) => {
    // Function body
};

Example:

const multiply = (a, b) => a * b;
console.log(multiply(4, 5));
// Output: 20

Arrow functions are very useful when there is just one line of code in the function body. And the brackets around a single parameter can also be removed if there is just one.


Higher-Order Functions in Javascript

Functions in JavaScript can be assigned to variables, returned from functions, or given as arguments to other functions because they are first-class citizens. Higher-order functions are those that return functions or accept other functions as arguments.

Higher-Order Functions in Javascript Example

function applyOperation(a, b, operation) {
    return operation(a, b);
}

function addition(x, y) {
    return x + y;
}

console.log(applyOperation(10, 5, addition));
// Output: 15


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad