Learn and master the top JavaScript Functions interview questions along with detailed answers and code snippets

  1. What is a function in JavaScript?
    A function in JavaScript is a block of reusable code that performs a specific task. It takes input, performs operations, and returns an output.
  2. How do you define a function in JavaScript?
    There are two ways to define a function in JavaScript: a. Function Declaration:
   function add(a, b) {
     return a + b;
   }

b. Function Expression:

   const add = function(a, b) {
     return a + b;
   };
  1. How do you call a function in JavaScript?
    To call a function in JavaScript, you simply write the function name followed by parentheses and any necessary arguments:
   function sayHello(name) {
     console.log('Hello, ' + name + '!');
   }

   sayHello('John'); // Output: Hello, John!
  1. What is the difference between parameters and arguments in a function?
    Parameters are the variables defined in the function declaration, while arguments are the values passed to the function when it is called.

    Parameters act as placeholders for the values that will be passed as arguments.
   function add(a, b) { // a and b are parameters
     return a + b;
   }

   const result = add(2, 3); // 2 and 3 are arguments
  1. How do you return a value from a function in JavaScript?
    You can use the return statement followed by the value you want to return from the function:
   function add(a, b) {
     return a + b;
   }

   const result = add(2, 3);
   console.log(result); // Output: 5
  1. What is a higher-order function in JavaScript?
    A higher-order function is a function that accepts another function as an argument or returns a function as its result. It allows for the composition and abstraction of functions.
  2. How do you pass a function as an argument to another function in JavaScript?
    You can pass a function as an argument to another function by simply using the function name without parentheses:
   function greet(name) {
     console.log('Hello, ' + name + '!');
   }

   function sayHelloToSomeone(greeting, name) {
     greeting(name);
   }

   sayHelloToSomeone(greet, 'John'); // Output: Hello, John!
  1. How do you assign a function to a variable in JavaScript?
    You can assign a function to a variable by using a function expression:
   const add = function(a, b) {
     return a + b;
   };

   const result = add(2, 3);
   console.log(result); // Output: 5
  1. What is a callback function in JavaScript?
    A callback function is a function that is passed as an argument to another function and is invoked at a later time or in response to an event. It allows for asynchronous and event-driven programming.
  2. How do you create a callback function in JavaScript?
    You can create a callback function by defining a function and passing it as an argument to another function:
   function process(callback) {
     // Do some processing
     callback();
   }

   function greet() {
     console.log('Hello!');
   }

   process(greet); // Output: Hello!
  1. What is an anonymous function in JavaScript?
    An anonymous function is a function without a name. It can be defined using a function expression:
   const add = function(a, b) {
     return a + b;
   };
  1. How do you invoke an anonymous function immediately?
    You can invoke an anonymous function immediately by wrapping it in parentheses and adding parentheses at the end to call it:
   (function() {
     console.log('I am invoked immediately!');
   })();
  1. What is a self-invoking anonymous function in JavaScript?
    A self-invoking anonymous function, also known as an immediately invoked function expression (IIFE), is a function that is invoked immediately after it is defined:
   (function() {
     console.log('I am invoked immediately!');
   })();
  1. How do you create a closure in JavaScript?
    A closure is created when a function is defined inside another function and has access to its parent function’s variables and scope, even after the parent function has finished executing:
   function outer() {
     const message = 'Hello';

     function inner() {
       console.log(message);
     }

     return inner;
   }

   const closure = outer();
   closure(); // Output: Hello
  1. How do you use the bind() method in JavaScript?
    The bind() method is used to create a new function with a specific this value and initial arguments.

    It allows you to bind a function to a particular object or set the context of the function:
   const person = {
     name: 'John',
     sayHello: function() {
       console.log('Hello, ' + this.name + '!');
     }
   };

   const greet = person.sayHello.bind(person);
   greet(); // Output: Hello, John!
  1. How do you use the apply() method in JavaScript?
    The apply() method is used to call a function with a given this value and an array or array-like object as arguments:
   function add(a, b) {
     return a + b;
   }

   const numbers = [2, 3];
   const sum = add.apply(null, numbers);
   console.log(sum); // Output: 5
  1. How do you use the call() method in JavaScript?
    The call() method is similar to the apply() method, but it accepts arguments individually rather than as an array or array-like object:
   function greet(name) {
     console.log('Hello, ' + name + '!');
   }

   greet.call(null, 'John'); // Output: Hello, John!
  1. How do you create a recursive function in JavaScript?
    A recursive function is a function that calls itself. It typically has a base case to terminate the recursion:
   function countdown(n) {
     if (n === 0) {
       console.log('Done!');
     } else {
       console.log(n);
       countdown(n - 1);
     }
   }

   countdown(5);
  1. How do you use the setTimeout() function in JavaScript?
    The setTimeout() function is used to schedule the execution of a function after a specified delay (in milliseconds):
   function sayHello() {
     console.log('Hello!');
   }

   setTimeout(sayHello, 2000); // Output: Hello! (after 2 seconds)
  1. How do you use the setInterval() function in JavaScript?
    The setInterval() function is used to repeatedly execute a function at a specified interval (in milliseconds):
   function printTime() {
     console.log(new Date().toLocaleTimeString());
   }

   setInterval(printTime,

 1000); // Output: Current time (updated every second)
  1. What is the difference between undefined and null in JavaScript?
    undefined is a value that is automatically assigned to a variable that has been declared but has not been assigned a value.

    null is an assignment value that represents no value or an empty value.
  2. What is the difference between a function declaration and a function expression in JavaScript?
    A function declaration is hoisted and can be used before it is declared in the code.

    A function expression is not hoisted and must be declared before it is used.
  3. What is the difference between a named function and an anonymous function in JavaScript?
    A named function has a name and can be referenced by its name.

    An anonymous function does not have a name and is typically used as a callback or immediately invoked.
  4. What is the arguments object in JavaScript?
    The arguments object is an array-like object that contains the arguments passed to a function.

    It allows you to access the arguments dynamically, regardless of the number of arguments defined in the function signature.
  5. What is the difference between call() and apply() methods in JavaScript?
    Both call() and apply() methods are used to invoke a function with a specified this value, but they differ in how they accept arguments.

    call() accepts arguments individually, while apply() accepts an array or array-like object of arguments.
  6. What is a rest parameter in JavaScript?
    A rest parameter allows a function to accept an indefinite number of arguments as an array.

    It is denoted by three dots (...) followed by the parameter name:
   function sum(...numbers) {
     return numbers.reduce((acc, curr) => acc + curr, 0);
   }

   console.log(sum(1, 2, 3, 4)); // Output: 10
  1. What is a default parameter in JavaScript?
    A default parameter allows you to specify a default value for a function parameter if no argument or undefined is passed in.

    It is denoted by assigning a value to the parameter in the function declaration:
   function greet(name = 'Guest') {
     console.log('Hello, ' + name + '!');
   }

   greet(); // Output: Hello, Guest!
   greet('John'); // Output: Hello, John!
  1. What is a generator function in JavaScript?
    A generator function is a special type of function that can be paused and resumed, allowing for the generation of a sequence of values over time.

    It is defined using the function* syntax:
   function* generateSequence() {
     yield 1;
     yield 2;
     yield 3;
   }

   const sequence = generateSequence();
   console.log(sequence.next().value); // Output: 1
   console.log(sequence.next().value); // Output: 2
   console.log(sequence.next().value); // Output: 3
  1. How do you handle errors in JavaScript functions?
    You can handle errors in JavaScript functions using the try...catch statement.

    The code inside the try block is executed, and if an error occurs, it is caught in the catch block:
   function divide(a, b) {
     try {
       if (b === 0) {
         throw new Error('Division by zero');
       }
       return a / b;
     } catch (error) {
       console.log(error.message);
     }
   }

   console.log(divide(10, 0)); // Output: Division by zero
  1. How do you write a pure function in JavaScript?
    A pure function is a function that always returns the same output for the same input and does not cause any side effects.

    It does not modify external state or rely on external variables.
   function add(a, b) {
     return a + b;
   }