TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses ยท 55.7K subscribers
Post #3402 2.5K
๐Ÿš€ JavaScript Interview Questions with Answers โ€” Part 2

11. What is a function in JavaScript? 
A function is a reusable block of code designed to perform a specific task.

Why Functions Are Important: 
โ€ข Reuse code
โ€ข Improve readability
โ€ข Reduce duplication
โ€ข Make programs modular

Syntax:

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


Calling a Function: greet();

Function With Parameters:

function greet(name) {
    console.log("Hello " + name);
}

greet("Deepak");


12. What is a function declaration vs expression? 

Function Declaration 
Defined using the function keyword with a name.

function add(a, b) {
    return a + b;
}


Function Expression 
Function stored inside a variable.

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


Feature Comparison: 
Hoisted โ†’ Declaration: Yes, Expression: No 
Named โ†’ Declaration: Usually, Expression: Can be anonymous 

Key Point: 
Function declarations can be called before they are defined because of hoisting.

13. What is an arrow function? 
Arrow functions are a shorter syntax for writing functions introduced in ES6.

Syntax:

const greet = () => {
    console.log("Hello");
};


Example With Parameters:

const add = (a, b) => a + b;
console.log(add(2, 3));


Benefits: 
โ€ข Shorter syntax
โ€ข Cleaner code
โ€ข No own this binding

Important: 
Arrow functions should not be used as object methods when this is required.

14. What is hoisting? 
Hoisting is JavaScriptโ€™s behavior of moving declarations to the top of the scope before execution.

Example:

console.log(a);
var a = 10;


Internally:

var a;
console.log(a);
a = 10;


Output: undefined

Important Points: 
โ€ข var is hoisted and initialized as undefined
โ€ข let and const are hoisted but stay in the Temporal Dead Zone (TDZ)

Function Hoisting:

sayHello();

function sayHello() {
    console.log("Hello");
}


15. What is a closure? 
A closure is created when an inner function remembers variables from its outer function even after the outer function has finished execution.

Example:

function outer() {
    let count = 0;

    return function inner() {
        count++;
        console.log(count);
    };
}

const counter = outer();

counter(); // 1
counter(); // 2


Why Closures Are Useful: 
โ€ข Data privacy
โ€ข Maintaining state
โ€ข Callbacks
โ€ข Memoization

Interview Definition: 
A closure gives a function access to its outer scope even after the outer function is executed.

16. What is the module pattern? 
The module pattern is used to create private and public variables/functions using closures.

Example:

const Counter = (function() {
    let count = 0;

    return {
        increment: function() {
            count++;
            console.log(count);
        },

        decrement: function() {
            count--;
            console.log(count);
        }
    };
})();


Counter.increment();
Counter.increment();

Benefits: 
โ€ข Encapsulation
โ€ข Data hiding
โ€ข Avoids global scope pollution

17. What is IIFE? 
IIFE stands for: 
Immediately Invoked Function Expression

It runs immediately after being created.

Syntax:

(function() {
    console.log("IIFE Executed");
})();


Arrow Function IIFE:

(() => {
    console.log("Hello");
})();


Why Use IIFE? 
โ€ข Avoid global variables
โ€ข Create private scope
โ€ข Execute code instantly

18. What is the difference between function parameters and arguments? 
Parameters โ†’ Variables in function definition 
Arguments โ†’ Actual values passed to function 

Example:

function greet(name) { // Parameter
    console.log(name);
}

greet("Deepak"); // Argument


Key Point: 
โ€ข Parameters receive values
โ€ข Arguments send values

19. What is a default parameter?
Default parameters allow functions to use a default value if no argument is passed.

Example:

function greet(name = "Guest") {
    console.log("Hello " + name);
}

greet();
greet("Deepak");


Output:

Hello Guest
Hello Deepak


Benefit: 
Prevents undefined values.

20. How do optional / rest parameters (...args) work? 
Rest parameters collect multiple arguments into a single array.
  • โค 6
More from @javascript_courses
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Oct 4, 2026DSA Handwritten Notes
  5. Sep 28, 2026To learn web design and development from basic to advanced levels, you can follow these stโ€ฆ
  6. Sep 27, 202610 Websites Every Developer & AI Enthusiast Should Bookmark โœ… roadmap.sh โ€“ Step-by-step leโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’