Function In  Javascript:

Function In Javascript:

The function is a block of code designed to perform some particular task

function myFunction(p1, p2) {
  return p1 * p2;
}

Anonymous functions:

In this, we can declare a function in the form of a variable, and hence can remove the name of function after function keyword

var f = function(a, b) {
  const sum = a + b;
  return sum;
}
console.log(f(3, 4));

Immediately invoked function :

You can create a function and call immediately call the function after declaring it.

const result = (function(a, b) {
  const sum = a + b;
  return sum;
})(3, 4);
console.log(result);

Functions Within Functions:

Here you can create nested functions inside a main function and could even return them

function createFunction() {
  function f(a, b) {
    const sum = a + b;
    return sum;
  }
  return f;
}
const f = createFunction();
console.log(f(3, 4));

Function Hoisting:

Javascript has a feature of hoisting where a function can sometimes be used before it is initialized. But it is considered as bad practice as it reduces code readability.

function createFunction() {
  return f;
  function f(a, b) {
    const sum = a + b;
    return sum;
  }
}
const f = createFunction();
console.log(f(3, 4));

Arrow functions:

It is a very common way to declare a function, here there is no need to write a function keyword.

const f = (a, b) => {
  const sum = a + b;
  return sum;
};
console.log(f(3, 4));