Functions in JavaScript

ยท

3 min read

A function is a reusable block of code designed to perform a specific task. You define a function once, and you can call (or "invoke") it as many times as needed, which helps reduce repetition and makes the code more modular and easier to maintain. It takes input(s), processes them, and optionally returns an output. Functions are standalone entities and are called directly by their name.

function greet() {
    console.log("Hello, World!");
}
greet() // "Hello, world!"

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

greet("priya") // "Hello, priya!"

//return value
function add(a, b) {
    return a + b;
}
add(4, 9) // 13

There are several types of functions in programming, categorized based on their behavior, parameters, and return values.

Here are some common types:

Function Definition

This is the most common type of function that is used.

//Syntax:
//Defining a function
function function_name(parameters-> not mendentory){
      //block of code
}

//example

function functionName(parameters) {
    // Code to be executed
    return value;  // Optional
}

Function Expression

//syntax
function variable_name =  function_name(){
      //block of code
}

const name = function fullname(fname, lname){
       console.log(`The firstname is ${fname} and lastname is ${lname}`);
}

name("ariana", "jonas");

Arrow Functions

//syntax 
function variable_name = () => {
    //block of code
}

//example:
const name = () => {
   console.log(`The firstname is ${fname} and lastname is ${lname}`);
};
name();

Higher order function

Functions that take another function as an argument, return a function, or both. These are commonly used in functional programming.

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

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

performOperation(add, 3, 4); // Output: 7

IIFE(Immediate invoke function)

A function that is executed immediately after it is defined. IIFEs are commonly used to create a local scope to avoid polluting the global scope.


(function () {
     console.log('Hello geeta');    
})();

Hoisting

Hoisting in JavaScript refers to the behavior where variable and function declarations are moved to the top of their containing scope (global or function scope) during the compilation phase, before the code is executed. This means you can use functions and variables before they are actually declared in the code.

greet();  // Output: "Hello, Geetika!"

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

Interview questions

  1. What is Function Declaration?

  2. What is Function Expression?

  3. What is Anonymous Function?

  4. What is IIFE?

  5. IIFE based Question

     (function(x){
         return (function (y) {
             console.log(x)    
     })(2)
     })(1)
    
  6. What is closures?

  7. What is function Scope?

  8. What is Hoisting?

  9. Parameter Vs Argument

  10. What is callback function?

  11. What is Arrow function?

  12. What is difference between Arrow function and Normal function?

ย