3. JavaScript Interview Questions (1–10) Interview Questions and Answers
Practice every question in this topic using expandable answers and interview tips. Answer aloud first, then open the explanation.
Back to the complete interview hub
Q1. What is JavaScript? [Easy]
Answer: JavaScript is a high-level, lightweight, interpreted programming language used to create dynamic and interactive web applications. It runs in web browsers and can also be used on servers with environments like Node.js.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q2. What are the features of JavaScript? [Easy]
Answer: JavaScript is lightweight, interpreted, dynamically typed, prototype-based, event-driven, object-oriented, supports functional programming, asynchronous programming, and works across all modern browsers.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q3. What is the difference between JavaScript and ECMAScript? [Easy]
Answer: ECMAScript (ES) is the standardized specification that defines the language. JavaScript is the most popular implementation of the ECMAScript standard used in browsers and server-side environments.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q4. How do you declare variables in JavaScript? [Easy]
Answer: Variables can be declared using var, let, and const. Modern JavaScript recommends using let for mutable variables and const for values that should not be reassigned.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q5. What is the difference between var, let, and const? [Medium]
Answer: var is function-scoped and allows redeclaration. let is block-scoped and allows reassignment but not redeclaration within the same scope. const is block-scoped and must be initialized during declaration; its reference cannot be reassigned.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q6. What are JavaScript data types? [Easy]
Answer: JavaScript has primitive data types: String, Number, Boolean, Undefined, Null, BigInt, and Symbol. It also has a non-primitive data type called Object, which includes arrays, functions, dates, and more.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q7. What is the difference between null and undefined? [Medium]
Answer: undefined means a variable has been declared but has not been assigned a value. null is an intentional assignment representing the absence of a value or an empty object reference.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q8. What is the difference between == and === operators? [Medium]
Answer: The == operator performs loose equality by converting operands to compatible types before comparison. The === operator performs strict equality by comparing both value and data type without type conversion.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q9. What are truthy and falsy values in JavaScript? [Medium]
Answer: Falsy values are false, 0, -0, 0n, empty string (""), null, undefined, and NaN. All other values are considered truthy and evaluate to true in boolean contexts.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q10. What is type coercion in JavaScript? [Medium]
Answer: Type coercion is JavaScript's automatic conversion of one data type into another during operations or comparisons. It can be implicit (performed automatically) or explicit (using functions such as Number(), String(), or Boolean()).
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q11. What is a function in JavaScript? [Easy]
Answer: A function is a reusable block of code designed to perform a specific task. Functions can accept parameters, execute statements, and optionally return a value.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q12. What is the difference between a function declaration and a function expression? [Easy]
Answer: A function declaration is hoisted completely, allowing it to be called before its definition. A function expression is assigned to a variable and is not fully hoisted, so it can only be called after it has been defined.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q13. What is an arrow function? [Medium]
Answer: An arrow function is a shorter syntax for writing functions introduced in ES6. It does not have its own 'this', 'arguments', 'super', or 'new.target' and instead inherits 'this' from its surrounding lexical scope.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q14. What is the difference between regular functions and arrow functions? [Hard]
Answer: Regular functions have their own 'this' context and can be used as constructors with the 'new' keyword. Arrow functions inherit 'this' from their enclosing scope, cannot be used as constructors, and do not have their own 'arguments' object.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q15. What is scope in JavaScript? [Medium]
Answer: Scope determines where variables and functions are accessible in a program. JavaScript provides global scope, function scope, block scope, and module scope.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q16. What is the difference between global scope, function scope, and block scope? [Medium]
Answer: Global scope variables are accessible throughout the application. Function scope variables are accessible only within the function where they are declared. Block scope variables, declared using let or const, are accessible only within the enclosing block such as an if statement or loop.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q17. What is hoisting in JavaScript? [Hard]
Answer: Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution. Function declarations are fully hoisted, var declarations are hoisted but initialized with undefined, while let and const are hoisted but remain inaccessible until initialization due to the Temporal Dead Zone.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q18. What is the Temporal Dead Zone (TDZ)? [Hard]
Answer: The Temporal Dead Zone is the period between entering a scope and initializing a let or const variable. Accessing the variable during this period throws a ReferenceError.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q19. What is a closure in JavaScript? [Hard]
Answer: A closure is created when an inner function retains access to variables from its outer lexical scope even after the outer function has finished executing. Closures are commonly used for data privacy, callbacks, event handlers, and function factories.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q20. What are practical uses of closures in JavaScript? [Hard]
Answer: Closures are used to create private variables, implement data encapsulation, build function factories, maintain state between function calls, create callbacks, event handlers, memoization, and module patterns.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q21. What is an object in JavaScript? [Easy]
Answer: An object is a collection of key-value pairs used to store related data and functionality. Properties store values, while methods are functions associated with the object.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q22. How do you create an object in JavaScript? [Medium]
Answer: Objects can be created using object literals ({}), the Object constructor, constructor functions, ES6 classes, or Object.create(). Object literals are the most common and recommended approach.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q23. What is the 'this' keyword in JavaScript? [Hard]
Answer: The 'this' keyword refers to the object that invokes a function. Its value depends on how the function is called. In arrow functions, 'this' is inherited from the surrounding lexical scope rather than being bound dynamically.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q24. What is an array in JavaScript? [Easy]
Answer: An array is an ordered collection of values that can store multiple data types. Arrays are zero-indexed, dynamically sized, and provide numerous built-in methods for data manipulation.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q25. What is the difference between map(), filter(), and reduce()? [Medium]
Answer: map() creates a new array by transforming each element. filter() creates a new array containing only elements that satisfy a condition. reduce() combines all elements into a single value such as a sum, object, or array.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q26. What is the difference between forEach() and map()? [Medium]
Answer: forEach() executes a function for each array element but does not return a new array. map() executes a function for each element and returns a new transformed array without modifying the original array.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q27. What is object destructuring in JavaScript? [Medium]
Answer: Object destructuring is an ES6 feature that extracts property values from an object into individual variables using concise syntax, improving code readability.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q28. What is the spread operator (...) in JavaScript? [Medium]
Answer: The spread operator expands iterable values such as arrays or objects into individual elements. It is commonly used for copying arrays, merging objects, and passing function arguments.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q29. What is the rest parameter (...) in JavaScript? [Medium]
Answer: The rest parameter collects multiple function arguments into a single array. It must be the last parameter in a function definition and simplifies handling variable numbers of arguments.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q30. What are optional chaining (?.) and nullish coalescing (??) operators? [Hard]
Answer: Optional chaining (?.) safely accesses nested object properties without throwing an error if a value is null or undefined. The nullish coalescing operator (??) returns the right-hand value only when the left-hand value is null or undefined, unlike || which also treats values such as 0, false, and an empty string as falsy.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q31. What is a callback function in JavaScript? [Medium]
Answer: A callback function is a function passed as an argument to another function and executed after a specific task completes. Callbacks are commonly used for asynchronous operations such as API requests, timers, and event handling.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q32. What is a Promise in JavaScript? [Medium]
Answer: A Promise is an object that represents the eventual completion or failure of an asynchronous operation. A Promise has three states: pending, fulfilled, and rejected.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q33. What is promise chaining? [Medium]
Answer: Promise chaining is the process of connecting multiple asynchronous operations using successive .then() methods. It helps execute asynchronous tasks in sequence while avoiding deeply nested callbacks.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q34. What is the difference between Promise.all(), Promise.race(), Promise.allSettled(), and Promise.any()? [Hard]
Answer: Promise.all() resolves when all promises succeed and rejects if any promise fails. Promise.race() settles as soon as the first promise settles. Promise.allSettled() waits for every promise to complete regardless of success or failure. Promise.any() resolves with the first fulfilled promise and rejects only if all promises are rejected.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q35. What is async/await in JavaScript? [Medium]
Answer: async and await are modern JavaScript features that simplify working with Promises. An async function always returns a Promise, and the await keyword pauses execution until the Promise is resolved or rejected.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q36. What is the Event Loop in JavaScript? [Hard]
Answer: The Event Loop is JavaScript's mechanism for handling asynchronous operations. It continuously checks the call stack and the task queues, moving queued tasks to the call stack when it becomes empty, enabling non-blocking execution.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q37. What is the Call Stack? [Hard]
Answer: The Call Stack is a data structure that keeps track of active function calls. Functions are pushed onto the stack when called and removed after execution. If too many nested calls occur, a 'Maximum call stack size exceeded' error is thrown.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q38. What is the difference between microtasks and macrotasks? [Hard]
Answer: Microtasks have higher priority and execute immediately after the current script finishes. Examples include Promise callbacks and queueMicrotask(). Macrotasks execute afterward and include setTimeout(), setInterval(), and DOM events.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q39. How does setTimeout() work in JavaScript? [Medium]
Answer: setTimeout() schedules a callback function to execute after at least the specified delay. The callback is placed in the macrotask queue and executes only when the call stack is empty and earlier tasks have completed.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q40. What is the Fetch API? [Medium]
Answer: The Fetch API is a modern browser interface for making HTTP requests. It returns a Promise and supports methods such as GET, POST, PUT, PATCH, and DELETE. Fetch is commonly used with async/await for cleaner asynchronous code.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q41. What is the DOM in JavaScript? [Easy]
Answer: The Document Object Model (DOM) is a tree-like representation of an HTML document. JavaScript uses the DOM to access, modify, create, and delete HTML elements dynamically.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q42. What is the difference between the DOM and the BOM? [Medium]
Answer: The DOM represents the HTML document and allows manipulation of page elements. The Browser Object Model (BOM) represents browser-specific objects such as window, navigator, history, screen, and location.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q43. How can you select HTML elements in JavaScript? [Medium]
Answer: Common DOM selection methods include getElementById(), getElementsByClassName(), getElementsByTagName(), querySelector(), and querySelectorAll(). The querySelector() methods support CSS selectors and are widely used in modern applications.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q44. What is event bubbling and event capturing? [Medium]
Answer: Event bubbling is the default event propagation mechanism where an event travels from the target element up to its ancestors. Event capturing is the opposite process, where the event travels from the root element down to the target element.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q45. What is event delegation and why is it useful? [Hard]
Answer: Event delegation is a technique where a single event listener is attached to a parent element instead of multiple child elements. It relies on event bubbling and improves performance, reduces memory usage, and supports dynamically added elements.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q46. What is the difference between preventDefault() and stopPropagation()? [Medium]
Answer: preventDefault() stops the browser's default behavior for an event, such as preventing a form submission or link navigation. stopPropagation() prevents the event from propagating to parent or child elements during event flow.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q47. What is the difference between localStorage and sessionStorage? [Medium]
Answer: localStorage stores data permanently until it is explicitly removed and remains available after the browser is closed. sessionStorage stores data only for the current browser tab or session and is cleared when the tab is closed.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q48. What is JSON? [Medium]
Answer: JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is commonly used for API communication.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q49. What is the difference between JSON.parse() and JSON.stringify()? [Medium]
Answer: JSON.parse() converts a JSON string into a JavaScript object. JSON.stringify() converts a JavaScript object into a JSON string for storage or transmission.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q50. How is error handling implemented in JavaScript? [Hard]
Answer: JavaScript uses try...catch...finally blocks for error handling. The try block contains code that may throw an exception, catch handles the error, and finally executes regardless of whether an error occurred. Custom errors can also be created using the throw statement and Error objects.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q51. What are JavaScript modules? [Medium]
Answer: JavaScript modules allow code to be split into reusable files. Each module has its own scope and can export variables, functions, or classes that can be imported into other modules, improving maintainability and code organization.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q52. What is the difference between export and import in JavaScript? [Medium]
Answer: The export keyword exposes variables, functions, classes, or objects from a module. The import keyword is used to access those exported members in another module. JavaScript supports both named exports and default exports.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q53. What is Strict Mode in JavaScript? [Medium]
Answer: Strict Mode is enabled by adding 'use strict'; at the beginning of a script or function. It helps detect common coding mistakes, prevents unsafe actions, throws errors for invalid operations, and encourages writing more secure and optimized code.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q54. What is the difference between call(), apply(), and bind()? [Hard]
Answer: call() invokes a function immediately with a specified 'this' value and individual arguments. apply() works similarly but accepts arguments as an array. bind() returns a new function with a permanently bound 'this' value without executing it immediately.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q55. What is debouncing in JavaScript? [Hard]
Answer: Debouncing delays the execution of a function until a specified period has passed since the last event. It is commonly used for search boxes, autocomplete, and form validation to reduce unnecessary function calls.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q56. What is throttling in JavaScript? [Hard]
Answer: Throttling limits how often a function can execute during a specified time interval. It is useful for handling high-frequency events such as scrolling, resizing, and mouse movement while maintaining application performance.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q57. What causes memory leaks in JavaScript and how can they be prevented? [Hard]
Answer: Memory leaks occur when objects are no longer needed but remain referenced in memory. Common causes include forgotten event listeners, uncleared timers, global variables, detached DOM elements, and unresolved closures. They can be prevented by removing unused references, clearing timers, and cleaning up event listeners.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q58. How does garbage collection work in JavaScript? [Hard]
Answer: JavaScript automatically manages memory using a garbage collector. It periodically identifies objects that are no longer reachable from the application and reclaims their memory. Modern JavaScript engines primarily use the mark-and-sweep algorithm.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q59. What are some commonly used JavaScript design patterns? [Hard]
Answer: Popular JavaScript design patterns include Module Pattern, Singleton Pattern, Factory Pattern, Observer Pattern, Prototype Pattern, MVC, MVVM, and Revealing Module Pattern. These patterns improve code organization, reusability, and maintainability.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Q60. What are JavaScript best practices for modern web development? [Medium]
Answer: Use let and const instead of var, prefer strict equality (===), write modular code, use async/await for asynchronous operations, avoid global variables, validate user input, handle errors with try...catch, optimize DOM manipulation, follow consistent coding standards, and write clean, reusable, and well-documented code.
Interview tip: Define it clearly, give a small example, and mention one practical use or limitation.
Study checklist
- Explain the answer without reading.
- Write one small example.
- Compare framework or database version differences in official documentation.
3. JavaScript Interview Questions (1–10) Interview Questions and Answers
Practice one question at a time. Read the question, answer aloud, then compare your explanation with the answer.
← Back to the complete interview hub
Q1. What is JavaScript?
Easy
Answer: JavaScript is a high-level, lightweight, interpreted programming language used to create dynamic and interactive web applications. It runs in web browsers and can also be used on servers with environments like Node.js.
Interview tip: Use a short example and connect the answer to real project work.
Q2. What are the features of JavaScript?
Easy
Answer: JavaScript is lightweight, interpreted, dynamically typed, prototype-based, event-driven, object-oriented, supports functional programming, asynchronous programming, and works across all modern browsers.
Interview tip: Use a short example and connect the answer to real project work.
Q3. What is the difference between JavaScript and ECMAScript?
Easy
Answer: ECMAScript (ES) is the standardized specification that defines the language. JavaScript is the most popular implementation of the ECMAScript standard used in browsers and server-side environments.
Interview tip: Use a short example and connect the answer to real project work.
Q4. How do you declare variables in JavaScript?
Easy
Answer: Variables can be declared using var, let, and const. Modern JavaScript recommends using let for mutable variables and const for values that should not be reassigned.
Interview tip: Use a short example and connect the answer to real project work.
Q5. What is the difference between var, let, and const?
Medium
Answer: var is function-scoped and allows redeclaration. let is block-scoped and allows reassignment but not redeclaration within the same scope. const is block-scoped and must be initialized during declaration; its reference cannot be reassigned.
Interview tip: Use a short example and connect the answer to real project work.
Q6. What are JavaScript data types?
Easy
Answer: JavaScript has primitive data types: String, Number, Boolean, Undefined, Null, BigInt, and Symbol. It also has a non-primitive data type called Object, which includes arrays, functions, dates, and more.
Interview tip: Use a short example and connect the answer to real project work.
Q7. What is the difference between null and undefined?
Medium
Answer: undefined means a variable has been declared but has not been assigned a value. null is an intentional assignment representing the absence of a value or an empty object reference.
Interview tip: Use a short example and connect the answer to real project work.
Q8. What is the difference between == and === operators?
Medium
Answer: The == operator performs loose equality by converting operands to compatible types before comparison. The === operator performs strict equality by comparing both value and data type without type conversion.
Interview tip: Use a short example and connect the answer to real project work.
Q9. What are truthy and falsy values in JavaScript?
Medium
Answer: Falsy values are false, 0, -0, 0n, empty string (""), null, undefined, and NaN. All other values are considered truthy and evaluate to true in boolean contexts.
Interview tip: Use a short example and connect the answer to real project work.
Q10. What is type coercion in JavaScript?
Medium
Answer: Type coercion is JavaScript's automatic conversion of one data type into another during operations or comparisons. It can be implicit (performed automatically) or explicit (using functions such as Number(), String(), or Boolean()).
Interview tip: Use a short example and connect the answer to real project work.
Q11. What is a function in JavaScript?
Easy
Answer: A function is a reusable block of code designed to perform a specific task. Functions can accept parameters, execute statements, and optionally return a value.
Interview tip: Use a short example and connect the answer to real project work.
Q12. What is the difference between a function declaration and a function expression?
Easy
Answer: A function declaration is hoisted completely, allowing it to be called before its definition. A function expression is assigned to a variable and is not fully hoisted, so it can only be called after it has been defined.
Interview tip: Use a short example and connect the answer to real project work.
Q13. What is an arrow function?
Medium
Answer: An arrow function is a shorter syntax for writing functions introduced in ES6. It does not have its own 'this', 'arguments', 'super', or 'new.target' and instead inherits 'this' from its surrounding lexical scope.
Interview tip: Use a short example and connect the answer to real project work.
Q14. What is the difference between regular functions and arrow functions?
Hard
Answer: Regular functions have their own 'this' context and can be used as constructors with the 'new' keyword. Arrow functions inherit 'this' from their enclosing scope, cannot be used as constructors, and do not have their own 'arguments' object.
Interview tip: Use a short example and connect the answer to real project work.
Q15. What is scope in JavaScript?
Medium
Answer: Scope determines where variables and functions are accessible in a program. JavaScript provides global scope, function scope, block scope, and module scope.
Interview tip: Use a short example and connect the answer to real project work.
Q16. What is the difference between global scope, function scope, and block scope?
Medium
Answer: Global scope variables are accessible throughout the application. Function scope variables are accessible only within the function where they are declared. Block scope variables, declared using let or const, are accessible only within the enclosing block such as an if statement or loop.
Interview tip: Use a short example and connect the answer to real project work.
Q17. What is hoisting in JavaScript?
Hard
Answer: Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution. Function declarations are fully hoisted, var declarations are hoisted but initialized with undefined, while let and const are hoisted but remain inaccessible until initialization due to the Temporal Dead Zone.
Interview tip: Use a short example and connect the answer to real project work.
Q18. What is the Temporal Dead Zone (TDZ)?
Hard
Answer: The Temporal Dead Zone is the period between entering a scope and initializing a let or const variable. Accessing the variable during this period throws a ReferenceError.
Interview tip: Use a short example and connect the answer to real project work.
Q19. What is a closure in JavaScript?
Hard
Answer: A closure is created when an inner function retains access to variables from its outer lexical scope even after the outer function has finished executing. Closures are commonly used for data privacy, callbacks, event handlers, and function factories.
Interview tip: Use a short example and connect the answer to real project work.
Q20. What are practical uses of closures in JavaScript?
Hard
Answer: Closures are used to create private variables, implement data encapsulation, build function factories, maintain state between function calls, create callbacks, event handlers, memoization, and module patterns.
Interview tip: Use a short example and connect the answer to real project work.
Q21. What is an object in JavaScript?
Easy
Answer: An object is a collection of key-value pairs used to store related data and functionality. Properties store values, while methods are functions associated with the object.
Interview tip: Use a short example and connect the answer to real project work.
Q22. How do you create an object in JavaScript?
Medium
Answer: Objects can be created using object literals ({}), the Object constructor, constructor functions, ES6 classes, or Object.create(). Object literals are the most common and recommended approach.
Interview tip: Use a short example and connect the answer to real project work.
Q23. What is the 'this' keyword in JavaScript?
Hard
Answer: The 'this' keyword refers to the object that invokes a function. Its value depends on how the function is called. In arrow functions, 'this' is inherited from the surrounding lexical scope rather than being bound dynamically.
Interview tip: Use a short example and connect the answer to real project work.
Q24. What is an array in JavaScript?
Easy
Answer: An array is an ordered collection of values that can store multiple data types. Arrays are zero-indexed, dynamically sized, and provide numerous built-in methods for data manipulation.
Interview tip: Use a short example and connect the answer to real project work.
Q25. What is the difference between map(), filter(), and reduce()?
Medium
Answer: map() creates a new array by transforming each element. filter() creates a new array containing only elements that satisfy a condition. reduce() combines all elements into a single value such as a sum, object, or array.
Interview tip: Use a short example and connect the answer to real project work.
Q26. What is the difference between forEach() and map()?
Medium
Answer: forEach() executes a function for each array element but does not return a new array. map() executes a function for each element and returns a new transformed array without modifying the original array.
Interview tip: Use a short example and connect the answer to real project work.
Q27. What is object destructuring in JavaScript?
Medium
Answer: Object destructuring is an ES6 feature that extracts property values from an object into individual variables using concise syntax, improving code readability.
Interview tip: Use a short example and connect the answer to real project work.
Q28. What is the spread operator (...) in JavaScript?
Medium
Answer: The spread operator expands iterable values such as arrays or objects into individual elements. It is commonly used for copying arrays, merging objects, and passing function arguments.
Interview tip: Use a short example and connect the answer to real project work.
Q29. What is the rest parameter (...) in JavaScript?
Medium
Answer: The rest parameter collects multiple function arguments into a single array. It must be the last parameter in a function definition and simplifies handling variable numbers of arguments.
Interview tip: Use a short example and connect the answer to real project work.
Q30. What are optional chaining (?.) and nullish coalescing (??) operators?
Hard
Answer: Optional chaining (?.) safely accesses nested object properties without throwing an error if a value is null or undefined. The nullish coalescing operator (??) returns the right-hand value only when the left-hand value is null or undefined, unlike || which also treats values such as 0, false, and an empty string as falsy.
Interview tip: Use a short example and connect the answer to real project work.
Q31. What is a callback function in JavaScript?
Medium
Answer: A callback function is a function passed as an argument to another function and executed after a specific task completes. Callbacks are commonly used for asynchronous operations such as API requests, timers, and event handling.
Interview tip: Use a short example and connect the answer to real project work.
Q32. What is a Promise in JavaScript?
Medium
Answer: A Promise is an object that represents the eventual completion or failure of an asynchronous operation. A Promise has three states: pending, fulfilled, and rejected.
Interview tip: Use a short example and connect the answer to real project work.
Q33. What is promise chaining?
Medium
Answer: Promise chaining is the process of connecting multiple asynchronous operations using successive .then() methods. It helps execute asynchronous tasks in sequence while avoiding deeply nested callbacks.
Interview tip: Use a short example and connect the answer to real project work.
Q34. What is the difference between Promise.all(), Promise.race(), Promise.allSettled(), and Promise.any()?
Hard
Answer: Promise.all() resolves when all promises succeed and rejects if any promise fails. Promise.race() settles as soon as the first promise settles. Promise.allSettled() waits for every promise to complete regardless of success or failure. Promise.any() resolves with the first fulfilled promise and rejects only if all promises are rejected.
Interview tip: Use a short example and connect the answer to real project work.
Q35. What is async/await in JavaScript?
Medium
Answer: async and await are modern JavaScript features that simplify working with Promises. An async function always returns a Promise, and the await keyword pauses execution until the Promise is resolved or rejected.
Interview tip: Use a short example and connect the answer to real project work.
Q36. What is the Event Loop in JavaScript?
Hard
Answer: The Event Loop is JavaScript's mechanism for handling asynchronous operations. It continuously checks the call stack and the task queues, moving queued tasks to the call stack when it becomes empty, enabling non-blocking execution.
Interview tip: Use a short example and connect the answer to real project work.
Q37. What is the Call Stack?
Hard
Answer: The Call Stack is a data structure that keeps track of active function calls. Functions are pushed onto the stack when called and removed after execution. If too many nested calls occur, a 'Maximum call stack size exceeded' error is thrown.
Interview tip: Use a short example and connect the answer to real project work.
Q38. What is the difference between microtasks and macrotasks?
Hard
Answer: Microtasks have higher priority and execute immediately after the current script finishes. Examples include Promise callbacks and queueMicrotask(). Macrotasks execute afterward and include setTimeout(), setInterval(), and DOM events.
Interview tip: Use a short example and connect the answer to real project work.
Q39. How does setTimeout() work in JavaScript?
Medium
Answer: setTimeout() schedules a callback function to execute after at least the specified delay. The callback is placed in the macrotask queue and executes only when the call stack is empty and earlier tasks have completed.
Interview tip: Use a short example and connect the answer to real project work.
Q40. What is the Fetch API?
Medium
Answer: The Fetch API is a modern browser interface for making HTTP requests. It returns a Promise and supports methods such as GET, POST, PUT, PATCH, and DELETE. Fetch is commonly used with async/await for cleaner asynchronous code.
Interview tip: Use a short example and connect the answer to real project work.
Q41. What is the DOM in JavaScript?
Easy
Answer: The Document Object Model (DOM) is a tree-like representation of an HTML document. JavaScript uses the DOM to access, modify, create, and delete HTML elements dynamically.
Interview tip: Use a short example and connect the answer to real project work.
Q42. What is the difference between the DOM and the BOM?
Medium
Answer: The DOM represents the HTML document and allows manipulation of page elements. The Browser Object Model (BOM) represents browser-specific objects such as window, navigator, history, screen, and location.
Interview tip: Use a short example and connect the answer to real project work.
Q43. How can you select HTML elements in JavaScript?
Medium
Answer: Common DOM selection methods include getElementById(), getElementsByClassName(), getElementsByTagName(), querySelector(), and querySelectorAll(). The querySelector() methods support CSS selectors and are widely used in modern applications.
Interview tip: Use a short example and connect the answer to real project work.
Q44. What is event bubbling and event capturing?
Medium
Answer: Event bubbling is the default event propagation mechanism where an event travels from the target element up to its ancestors. Event capturing is the opposite process, where the event travels from the root element down to the target element.
Interview tip: Use a short example and connect the answer to real project work.
Q45. What is event delegation and why is it useful?
Hard
Answer: Event delegation is a technique where a single event listener is attached to a parent element instead of multiple child elements. It relies on event bubbling and improves performance, reduces memory usage, and supports dynamically added elements.
Interview tip: Use a short example and connect the answer to real project work.
Q46. What is the difference between preventDefault() and stopPropagation()?
Medium
Answer: preventDefault() stops the browser's default behavior for an event, such as preventing a form submission or link navigation. stopPropagation() prevents the event from propagating to parent or child elements during event flow.
Interview tip: Use a short example and connect the answer to real project work.
Q47. What is the difference between localStorage and sessionStorage?
Medium
Answer: localStorage stores data permanently until it is explicitly removed and remains available after the browser is closed. sessionStorage stores data only for the current browser tab or session and is cleared when the tab is closed.
Interview tip: Use a short example and connect the answer to real project work.
Q48. What is JSON?
Medium
Answer: JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is commonly used for API communication.
Interview tip: Use a short example and connect the answer to real project work.
Q49. What is the difference between JSON.parse() and JSON.stringify()?
Medium
Answer: JSON.parse() converts a JSON string into a JavaScript object. JSON.stringify() converts a JavaScript object into a JSON string for storage or transmission.
Interview tip: Use a short example and connect the answer to real project work.
Q50. How is error handling implemented in JavaScript?
Hard
Answer: JavaScript uses try...catch...finally blocks for error handling. The try block contains code that may throw an exception, catch handles the error, and finally executes regardless of whether an error occurred. Custom errors can also be created using the throw statement and Error objects.
Interview tip: Use a short example and connect the answer to real project work.
Q51. What are JavaScript modules?
Medium
Answer: JavaScript modules allow code to be split into reusable files. Each module has its own scope and can export variables, functions, or classes that can be imported into other modules, improving maintainability and code organization.
Interview tip: Use a short example and connect the answer to real project work.
Q52. What is the difference between export and import in JavaScript?
Medium
Answer: The export keyword exposes variables, functions, classes, or objects from a module. The import keyword is used to access those exported members in another module. JavaScript supports both named exports and default exports.
Interview tip: Use a short example and connect the answer to real project work.
Q53. What is Strict Mode in JavaScript?
Medium
Answer: Strict Mode is enabled by adding 'use strict'; at the beginning of a script or function. It helps detect common coding mistakes, prevents unsafe actions, throws errors for invalid operations, and encourages writing more secure and optimized code.
Interview tip: Use a short example and connect the answer to real project work.
Q54. What is the difference between call(), apply(), and bind()?
Hard
Answer: call() invokes a function immediately with a specified 'this' value and individual arguments. apply() works similarly but accepts arguments as an array. bind() returns a new function with a permanently bound 'this' value without executing it immediately.
Interview tip: Use a short example and connect the answer to real project work.
Q55. What is debouncing in JavaScript?
Hard
Answer: Debouncing delays the execution of a function until a specified period has passed since the last event. It is commonly used for search boxes, autocomplete, and form validation to reduce unnecessary function calls.
Interview tip: Use a short example and connect the answer to real project work.
Q56. What is throttling in JavaScript?
Hard
Answer: Throttling limits how often a function can execute during a specified time interval. It is useful for handling high-frequency events such as scrolling, resizing, and mouse movement while maintaining application performance.
Interview tip: Use a short example and connect the answer to real project work.
Q57. What causes memory leaks in JavaScript and how can they be prevented?
Hard
Answer: Memory leaks occur when objects are no longer needed but remain referenced in memory. Common causes include forgotten event listeners, uncleared timers, global variables, detached DOM elements, and unresolved closures. They can be prevented by removing unused references, clearing timers, and cleaning up event listeners.
Interview tip: Use a short example and connect the answer to real project work.
Q58. How does garbage collection work in JavaScript?
Hard
Answer: JavaScript automatically manages memory using a garbage collector. It periodically identifies objects that are no longer reachable from the application and reclaims their memory. Modern JavaScript engines primarily use the mark-and-sweep algorithm.
Interview tip: Use a short example and connect the answer to real project work.
Q59. What are some commonly used JavaScript design patterns?
Hard
Answer: Popular JavaScript design patterns include Module Pattern, Singleton Pattern, Factory Pattern, Observer Pattern, Prototype Pattern, MVC, MVVM, and Revealing Module Pattern. These patterns improve code organization, reusability, and maintainability.
Interview tip: Use a short example and connect the answer to real project work.
Q60. What are JavaScript best practices for modern web development?
Medium
Answer: Use let and const instead of var, prefer strict equality (===), write modular code, use async/await for asynchronous operations, avoid global variables, validate user input, handle errors with try...catch, optimize DOM manipulation, follow consistent coding standards, and write clean, reusable, and well-documented code.
Interview tip: Use a short example and connect the answer to real project work.
Quick revision checklist
- Explain the answer without reading.
- Write one small example.
- Review platform or version differences in official documentation.
No comments:
Post a Comment
Please do not enter any spam link in the comment box.