Functions

Q. What is Function ?

Function is JS are reusable block of code that performs certain tasks. It can be used many times in our code.

Q. What is Function Statement / Function Declaration ?

It is used to define a function using the naming conventions. It is hoisted at the top of their respective scope.

function functionName(parameters) {
  // code to be executed
}

Q. What is Function expression ?

A function expression is a way to define a function as part of an expression making it versatile for assigning to variables, passing as arguments, or invoking immediately.

The variable that hold the function is hoisted as undefined in it’s respective scope. When the execution of the code reach that line, only then the function is assigned to that variable.

const fName = function (params) {
  // function body
};

// exmaple
var x = function (y) {
  return y * y;
};

Q. What is Anonymous Function?

An anonymous function is simply a function that does not have a name. Anonymous function are used as values.

<aside> 💡

i.e. we can use anonymous function to assign it to a variable.

IIFE is another good example.

</aside>

function() {
    // Function Body
 }
 
 // we may also declare anonmuous function using arrow function
 ( () => {
    // Function Body...
})();