JavaScript Interview Questions

Curated questions for junior and senior roles. Use the filter to jump to what you need.

Showing 197 of 197

JavaScript Interview Questions for Junior Developers

JavaScript powers interactive, dynamic experiences on the web and beyond. These questions cover fundamentals—types, scope, closures, DOM APIs, async patterns, and browser quirks.

  1. What is the difference between var, let, and const in JavaScript?
    `var` is function scoped, while `let` and `const` are block scoped. `var` is hoisted to the top of its function, while `let` and `const` are not. `const` is used for variables that cannot be reassigned, while `let` is used for variables that can be updated.
  2. What is closure in JavaScript?
    A closure is a function that has access to variables in its outer scope even after the outer function has returned.
  3. What is event bubbling in JavaScript?
    Event bubbling is a mechanism in JavaScript where an event fired on an element propagates up to its parent elements, until it reaches the document element. It allows a single event handler to be attached to multiple elements.
  4. What is hoisting in JavaScript?
    Hoisting is a mechanism in JavaScript where variables and function declarations are moved to the top of their scope. Variables declared with `var` are hoisted, but variables declared with `let` and `const` are not.
  5. What is the difference between null and undefined in JavaScript?
    `undefined` means a variable has been declared but has not been assigned a value. `null` is a value that represents the intentional absence of any object value.
  6. What is a Promise in JavaScript?
    A Promise is an object representing the eventual completion or failure of an asynchronous operation. It provides a way to register callbacks to be called when the promise is resolved or rejected.
  7. What is a pure function in JavaScript?
    A pure function is a function in JavaScript that, given the same inputs, will always return the same output and does not have any side effects, such as modifying variables outside its scope or making API calls.
  8. What is the difference between .map() and .forEach() in JavaScript?
    .`map()` returns a new array with the results of calling a provided function on every element in the calling array. `forEach()` is used to execute a function on each element in an array, but does not return a new array.
  9. What is the difference between synchronous and asynchronous code in JavaScript?
    Synchronous code is executed in order, blocking further execution until it's finished. Asynchronous code is executed outside the main thread, allowing other code to run in parallel, and does not block the execution of subsequent code.
  10. What is the difference between a class and an object in JavaScript?
    A class is a blueprint for creating objects, providing a way to create objects with similar attributes and methods. An object is an instance of a class, and can have its own values for the attributes defined in the class.
  11. What is the difference between == and Object.is() in JavaScript?
    `==` performs type coercion, while `Object.is()` does not. `Object.is()` is used to determine whether two values are the same value, and it considers `NaN` equal to `NaN` and `0` and `-0` as different values.
  12. What is a generator function in JavaScript?
    A generator function is a special type of function in JavaScript that can be paused and resumed, allowing it to produce a series of values over time. It is defined using the `function*` syntax and uses the `yield` keyword to produce values.
  13. What is event propagation in JavaScript?
    Event propagation is a mechanism in JavaScript where an event fired on an element propagates through the element's ancestors to the document element. It can be either bubbling (propagating from the innermost element to the outermost) or capturing (propagating from the outermost to the innermost).
  14. What is a currying function in JavaScript?
    A currying function is a higher-order function that returns a new function with some of its arguments already set. This allows the returned function to be partially applied, taking fewer arguments and returning a new function that can be invoked with the remaining arguments.
  15. What is closure in JavaScript?
    A closure is an inner function that has access to the outer (enclosing) function's variables and parameters, even after the outer function has returned. The closure has access to variables in its outer scope, even after the parent function has finished executing.
  16. What is hoisting in JavaScript?
    Hoisting is a mechanism in JavaScript where variable and function declarations are moved to the top of their respective scopes, regardless of where they are declared in the code. This means that variables and functions can be used before they are declared, but their value will be `undefined` until their declaration is processed.
  17. What is the difference between let and var in JavaScript?
    `var` is function scoped, while `let` and `const` are block scoped. This means that variables declared with `var` are accessible within the entire function, while variables declared with `let` and `const` are only accessible within the block in which they are declared.
  18. What is a proxy in JavaScript?
    A proxy in JavaScript is an object that wraps another object and controls access to that object. Proxies are used to intercept and modify operations on objects, such as getting and setting properties and calling methods, allowing you to implement custom behavior for these operations.
  19. What is a decorator in JavaScript?
    A decorator in JavaScript is a special syntax for attaching behavior to a class, method, property, or parameter. Decorators are a way to modify the behavior of a class or its members, allowing you to add or change functionality without having to write a lot of extra code.
  20. What is a higher-order function in JavaScript?
    A higher-order function in JavaScript is a function that either takes one or more functions as arguments, returns a function as its result, or both. Higher-order functions are a powerful feature of functional programming, allowing you to abstract and reuse common logic.
  21. What is a promise in JavaScript?
    A promise in JavaScript is an object that represents the result of an asynchronous operation. A promise can be in one of three states: pending, resolved, or rejected. Once a promise is resolved or rejected, its value can no longer be changed.
  22. What is the difference between null and undefined in JavaScript?
    `undefined` means that a variable has been declared, but has not yet been assigned a value. `null` is an assignment value, which represents no value or no object.
  23. What is the event loop in JavaScript?
    The event loop in JavaScript is a mechanism for scheduling and executing code, allowing the browser to continue running other code while waiting for asynchronous events, such as network requests, to complete. The event loop checks the message queue for incoming messages, and executes the associated callback functions in the order they were added to the queue.
  24. What is a module in JavaScript?
    A module in JavaScript is a piece of code that is executed once it is loaded, and has its own scope, allowing you to declare variables and functions that are not accessible from the outside. Modules are used to organize and reuse code, and can be exported and imported in other parts of your application.
  25. What is prototype-based inheritance in JavaScript?
    Prototype-based inheritance in JavaScript is a mechanism for creating objects that inherit properties and methods from other objects. In JavaScript, every object has a prototype, which is used to resolve property lookups when they are not found on the object itself. By setting the prototype of one object to be another object, you can create inheritance relationships between objects.
  26. What is a spread operator in JavaScript?
    The spread operator in JavaScript is denoted by three dots (`...`), and allows you to spread an array or iterable object into individual elements. The spread operator can be used in array literals, function calls, and destructuring assignments, among other places.
  27. What is a generator in JavaScript?
    A generator in JavaScript is a special type of function that can be paused and resumed at any point, allowing you to create an iterator that can be used to produce a sequence of values. Generators are useful for producing sequences of values on demand, such as the Fibonacci sequence, or for breaking up long-running computations into smaller, more manageable chunks.
  28. What is the difference between Array.map() and Array.forEach() in JavaScript?
    `Array.map()` returns a new array with the results of calling a provided function on every element in the original array, while `Array.forEach()` executes a provided function on each element in the original array, but does not return a new array. The main difference is that `Array.map()` returns a new array, while `Array.forEach()` modifies the original array in place.
  29. What is a closure in JavaScript?
    A closure in JavaScript is a function that has access to variables in its outer scope, even after the outer function has returned. Closures allow you to create private variables and functions, and to maintain state across multiple function calls.
  30. What is event bubbling in JavaScript?
    Event bubbling in JavaScript refers to the behavior where an event triggered on a child element will also trigger on its parent elements, propagating up the DOM tree. By default, events in JavaScript bubble up the DOM tree, allowing you to handle events on parent elements, but also making it possible to interfere with other event handlers.
  31. What is event capturing in JavaScript?
    Event capturing in JavaScript refers to a mechanism where an event triggered on a child element will first trigger on its parent elements, propagating down the DOM tree. Event capturing is the opposite of event bubbling, and allows you to handle events on parent elements before they are handled on child elements.
  32. What is hoisting in JavaScript?
    Hoisting in JavaScript refers to the behavior where variable and function declarations are moved to the top of their respective scopes, effectively making them available throughout the entire scope. This behavior can lead to unexpected results if you reference a variable before declaring it, as the variable will be declared at the top of the scope, but will not have a value until it is assigned later in the code.
  33. What is the difference between let and var in JavaScript?
    `var` is function scoped, meaning that a variable declared with `var` is accessible within the entire function, regardless of block scope. `let` is block scoped, meaning that a variable declared with `let` is only accessible within the block it is declared in, and any nested blocks.
  34. What is a Promise in JavaScript?
    A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. Promises provide a way to register callbacks to be called when the asynchronous operation is complete, or if it fails. Promises are used to handle asynchronous code in a more manageable and predictable way.
  35. What is the difference between null and undefined in JavaScript?
    `undefined` means a variable has been declared but has not yet been assigned a value, while `null` is an assignment value, it can be assigned to a variable to represent no value.
  36. What is the difference between const and let in JavaScript?
    `let` is similar to `var`, but is block scoped, while `const` is also block scoped but its value cannot be reassigned.
  37. What is a higher-order function in JavaScript?
    A higher-order function in JavaScript is a function that takes one or more functions as arguments, and/or returns a function as its result. Higher-order functions are a fundamental concept in functional programming, and allow you to abstract and compose functions in powerful ways.
  38. What is an IIFE in JavaScript?
    An IIFE (Immediately Invoked Function Expression) in JavaScript is a function that is immediately executed as soon as it is defined. IIFEs are often used to create a private scope and to avoid polluting the global scope with variables and functions.
  39. What is the difference between call and apply in JavaScript?
    Both `call` and `apply` are methods for calling a function and setting its `this` value, but the difference is in the way that they accept arguments. `call` accepts arguments separately, while `apply` accepts arguments as an array.
  40. What is the difference between null and undefined in JavaScript?
    `undefined` means a variable has been declared but has not yet been assigned a value, while `null` is an assignment value, it can be assigned to a variable to represent no value.
  41. What is a ternary operator in JavaScript?
    A ternary operator in JavaScript is a shorthand way of writing an `if`-`else` statement. The ternary operator has the form `condition ? value1 : value2`, and returns `value1` if `condition` is truthy, and `value2` if `condition` is falsy.
  42. What is a spread operator in JavaScript?
    The spread operator (`...`) in JavaScript allows you to spread an array into individual elements, or to spread the properties of an object into a new object. The spread operator is also used to merge arrays, or to pass an array of values as arguments to a function.
  43. What is hoisting in JavaScript?
    Hoisting in JavaScript is the behavior of moving variable and function declarations to the top of their respective scopes, so that they can be used before they are declared. Only the declaration is hoisted, not the assignment, so a variable declared with `var` will be undefined until its assignment statement is executed.
  44. What is closure in JavaScript?
    A closure in JavaScript is a function that has access to variables in its outer scope, even after the outer function has returned. Closures are created when a function is defined inside another function, and they allow the inner function to preserve its state even after the outer function has completed its execution.
  45. What is event bubbling in JavaScript?
    Event bubbling in JavaScript is the behavior where an event triggered on an element propagates up the DOM tree to its parent elements. This allows you to handle events on multiple elements in a single event handler, rather than having to attach an individual event handler to each element.
  46. What is event capturing in JavaScript?
    Event capturing in JavaScript is the process where an event is first captured by the outermost element and then propagated down to the inner elements. This is the opposite of event bubbling, and allows you to handle events on the outermost element before they reach the inner elements.
  47. What is a prototype in JavaScript?
    A prototype in JavaScript is an object that serves as a blueprint for creating other objects. Every object in JavaScript has a prototype, and you can use the prototype to add properties and methods to an object, so that all objects created from that prototype will inherit those properties and methods.
  48. What is a Promise in JavaScript?
    A Promise in JavaScript is an object that represents the eventual result of an asynchronous operation. Promises provide a way to register callback functions that will be executed when the asynchronous operation has completed or if it has failed. Promises can be used to write cleaner and more readable asynchronous code.
  49. What is the difference between == and === in JavaScript?
    The double equal sign (`==`) in JavaScript performs type coercion before comparing two values, while the triple equal sign (`===`) compares two values without type coercion. This means that `==` will attempt to convert the operands to the same type before making the comparison, while `===` requires the operands to have the same type and value to be considered equal.
  50. What is the purpose of 'use strict' in JavaScript?
    The `'use strict'` directive in JavaScript is used to indicate that the code should be executed in strict mode. Strict mode makes several changes to normal JavaScript semantics, such as disallowing the use of implicit global variables, and making it harder to accidentally overwrite global objects and properties.
  51. What is a generator function in JavaScript?
    A generator function in JavaScript is a special type of function that can be paused and resumed during its execution. Generator functions are defined using the `function*` syntax, and they return an iterator that can be used to step through the function's code, one step at a time. Generator functions are often used to create asynchronous code that can be paused and resumed, without blocking the main thread of execution.
  52. What is a module in JavaScript?
    A module in JavaScript is a reusable block of code that exports values, functions, or objects for use in other parts of an application. Modules allow you to organize your code into smaller, more manageable chunks, and to share code between different parts of an application. Modules can be imported and used in other modules using various module systems, such as CommonJS, AMD, and ECMAScript Modules (ESM).
  53. What is the difference between null and undefined in JavaScript?
    In JavaScript, `undefined` and `null` are both used to represent absence of a value, but they have slightly different meanings. `undefined` is a value that is assigned to a variable that has been declared but has not been assigned a value. `null` is an explicit value that can be assigned to a variable to indicate that it does not have a value.
  54. What is an object destructuring in JavaScript?
    Object destructuring in JavaScript is a shorthand syntax for extracting values from an object and assigning them to variables. It allows you to extract values from an object and assign them to individual variables, in a single statement, rather than having to extract the values one by one.
  55. What is a spread operator in JavaScript?
    The spread operator in JavaScript is denoted by three dots (`...`), and is used to spread the values of an array, object, or iterable into a new array or object. The spread operator can be used in various ways, such as to concatenate arrays, to spread an array into function arguments, or to copy an object by spreading its properties into a new object.
  56. What is a map in JavaScript?
    A map in JavaScript is a collection of key-value pairs, where each key is unique. Maps are similar to objects, but have several important differences, such as the ability to use any value (not just strings) as a key, and the ability to iterate over the keys and values in the map. Maps are useful when you need to maintain a collection of values that are associated with keys, and you need to look up values by key.
  57. What is a Set in JavaScript?
    A Set in JavaScript is a collection of unique values, without any duplicates. Sets are similar to arrays, but have several important differences, such as the ability to add and remove values, and the ability to easily check if a value is in the set or not. Sets are useful when you need to maintain a collection of unique values, and you need to perform operations such as union, intersection, and difference.
  58. What is a closure in JavaScript?
    A closure in JavaScript is a function that has access to its outer scope even after the outer function has returned. Closures are created when a function is declared inside another function, and they allow the inner function to reference variables in the outer function even after the outer function has returned. Closures are useful for creating function factories, for implementing private variables, and for preserving state between function calls.
  59. What is event bubbling in JavaScript?
    Event bubbling in JavaScript is a mechanism for event propagation, where an event that is triggered on an element bubbles up to its parent elements and ancestors, unless it is stopped. Event bubbling allows you to handle events on multiple elements in the same event handler, without having to attach a separate event handler to each element.
  60. What is event capturing in JavaScript?
    Event capturing in JavaScript is the opposite of event bubbling, where an event is first captured by the outermost element and then propagates down to the inner elements. Event capturing allows you to handle events on elements that are lower in the hierarchy before they reach the target element.
  61. What is a promise in JavaScript?
    A promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. Promises provide a way to register callbacks to be called when the asynchronous operation is complete or fails, and they can be used to simplify complex chains of asynchronous operations. Promises can be either resolved (fulfilled) with a value or rejected with an error, and they provide a clean and simple way to handle asynchronous code.
  62. What is the difference between let and var in JavaScript?
    In JavaScript, `var` and `let` are both used to declare variables, but they have different scoping rules. `var` variables are function-scoped, meaning that they are accessible within the function where they are declared, as well as any nested functions. `let` variables are block-scoped, meaning that they are only accessible within the block where they are declared, as well as any nested blocks.
  63. What is the difference between null and undefined in JavaScript?
    In JavaScript, both `null` and `undefined` represent absence of a value, but they are different values and are used in different situations. `undefined` is used to indicate that a variable has been declared but has not been assigned a value, while `null` is used to indicate explicitly that a variable has no value. When a function does not return a value, it is automatically considered to return `undefined`.
  64. What is hoisting in JavaScript?
    Hoisting in JavaScript is the behavior of moving variable and function declarations to the top of their respective scopes, regardless of where they are declared in the code. This means that variable and function declarations are processed before any other code is executed, allowing them to be used in the entire scope, even before they are declared. Hoisting is only applicable to variable and function declarations, and not to variable assignments.
  65. What is the prototype in JavaScript?
    The prototype in JavaScript is a property of an object that references another object. Every object in JavaScript has a prototype, which is an object from which it can inherit properties. When you access a property on an object, JavaScript first looks for the property on the object itself, and if it is not found, it continues to look for the property on the object's prototype, and so on, until it reaches the top-level `Object.prototype`. This mechanism is called prototypal inheritance and it is one of the key features of the JavaScript language.
  66. What is a template literal in JavaScript?
    A template literal in JavaScript is a special type of string literal that is surrounded by backticks (`) instead of quotes. Template literals allow you to embed expressions and variables directly inside a string, without having to use concatenation or string conversion. Template literals also support multiline strings, making it easier to create formatted strings, and they automatically escape characters, making them more secure than traditional string concatenation.
  67. What is a closure in JavaScript?
    A closure in JavaScript is a function that has access to variables in its outer scope, even after the outer function has returned. A closure is created when a function is defined inside another function, and the inner function has access to the variables and arguments of the outer function. The inner function can reference and manipulate these variables even after the outer function has returned, allowing for a persistent scope and a way to create private variables.
  68. What is a Promise in JavaScript?
    A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. Promises provide a way to register callbacks to be called when the async operation is complete or fails, and they can be used to handle asynchronous operations in a cleaner and more organized way than traditional callback-based approaches. Promises can be in one of three states: `pending`, `fulfilled`, or `rejected`. Once a Promise is fulfilled or rejected, it cannot be changed to a different state.
  69. What is an event loop in JavaScript?
    The event loop in JavaScript is a mechanism that continually checks the message queue and processes each message, one at a time. The event loop is what allows JavaScript to be non-blocking and to handle multiple tasks concurrently. When a task is completed, it is pushed to the message queue and the event loop takes care of processing the next task. This allows JavaScript to handle user input, network requests, timers, and other asynchronous tasks without blocking the main thread and freezing the user interface.
  70. What is the difference between let and var in JavaScript?
    `var` and `let` are both used to declare variables in JavaScript, but they have different scoping rules. Variables declared with `var` are function-scoped, meaning that they are only accessible within the function in which they are declared. Variables declared with `let` are block-scoped, meaning that they are only accessible within the block in which they are declared, and they are not hoisted to the top of their scope like variables declared with `var`. It is recommended to use `let` instead of `var` for declaring variables, as block scoping can help prevent variable naming conflicts and accidental reuse of variables.
  71. What is a weakmap in JavaScript?
    A WeakMap in JavaScript is a collection of key-value pairs where the keys are objects and the values can be any value. Unlike a regular Map, a WeakMap does not prevent its keys from being garbage collected, meaning that if there are no other references to a key, it can be automatically removed from the WeakMap. This makes WeakMaps useful for creating private properties, as the keys can be objects that are only referenced within a specific scope and can be automatically cleaned up when they are no longer needed.
  72. What is a map in JavaScript?
    A Map in JavaScript is a collection of key-value pairs, similar to an object. However, maps have a few advantages over objects: - The keys in a map can be any value (not just strings like in an object) - The order of elements in a map is guaranteed, unlike in an object where the order is not guaranteed - Maps have built-in methods for adding, removing, and checking the existence of elements, whereas with an object you would need to write your own methods to do these things.
  73. What is an arrow function in JavaScript?
    An arrow function in JavaScript is a shorthand for defining a function expression. Arrow functions have a more concise syntax compared to regular functions, and they also have different rules for `this` binding. In an arrow function, `this` is lexically scoped, meaning it takes the value of `this` from the surrounding code. This makes arrow functions especially useful in scenarios where you need to preserve the value of `this` from a surrounding scope.
  74. What is hoisting in JavaScript?
    Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top of their scope, before any code is executed. This means that a variable or function declared anywhere in a code block can be used before it is declared. In practice, this means that it is best to declare variables and functions at the top of their scope to avoid confusion and unexpected behavior.
  75. What is an async function in JavaScript?
    An async function in JavaScript is a function that is declared with the `async` keyword. When a function is declared as async, it returns a Promise that is automatically resolved with the value returned by the function or rejected with an error thrown from the function. Async functions make it easy to write asynchronous code that is organized and easy to read. You can use `await` inside an async function to pause the execution of the function until a Promise is resolved or rejected.
  76. What is a spread operator in JavaScript?
    The spread operator in JavaScript is represented by three dots (`...`). It is used to spread the elements of an iterable (e.g. an array) into a new array, or to spread the properties of an object into a new object. The spread operator can also be used to pass multiple arguments to a function as separate parameters, instead of passing an array or an object.
  77. What is a closure in JavaScript?
    A closure in JavaScript is an inner function that has access to variables in the outer (enclosing) function's scope, even after the outer function has returned. Closures are used to preserve state and to create private variables, because the inner function has access to variables in the outer function's scope even after the outer function has returned.
  78. What is the difference between let and var in JavaScript?
    `var` is function scoped, which means that variables declared with `var` are accessible within the entire function. On the other hand, `let` is block scoped, which means that variables declared with `let` are only accessible within the block they are declared in. Additionally, `var` variables are hoisted to the top of their scope, while `let` variables are not hoisted. This means that a `var` variable can be used before it is declared, while a `let` variable must be declared before it can be used.
  79. What is the difference between null and undefined in JavaScript?
    `undefined` means that a variable has been declared but has not been assigned a value. On the other hand, `null` is a value that represents the intentional absence of any object value. It is often used to represent the absence of a value in an object or an array. In other words, `undefined` is a value that is automatically assigned to a variable that has been declared but has not been assigned a value, while `null` is a value that must be explicitly assigned to a variable.
  80. What is a prototype in JavaScript?
    In JavaScript, a prototype is an object that is used as a blueprint for creating other objects. When an object is created, it inherits all the properties and methods of its prototype. If an object does not have a property, it will look up the prototype chain until it finds the property or reaches the end of the chain. The prototype of an object can be accessed using the `__proto__` property or the `Object.getPrototypeOf()` method.
  81. What is a higher-order function in JavaScript?
    A higher-order function in JavaScript is a function that takes one or more functions as arguments and/or returns a function. Higher-order functions are a key aspect of functional programming, and they are used to abstract over actions, rather than describing a specific action to perform. They can be used to create more reusable and composable code, making it easier to write and maintain.
  82. What is the difference between null and NaN in JavaScript?
    `null` is a value that represents the intentional absence of any object value. On the other hand, `NaN` (Not a Number) is a special numeric value that represents the result of a mathematical operation that cannot be represented as a number. `NaN` is used to indicate an error, such as trying to divide by zero. `NaN` is not equal to any value, including itself, so it is necessary to use the `isNaN()` function to test if a value is `NaN`.
  83. What is an anonymous function in JavaScript?
    An anonymous function in JavaScript is a function that is declared without a name. Anonymous functions are often used as arguments for higher-order functions or as self-executing functions, because they don't need to be referenced outside of their scope. Anonymous functions can be declared using the function keyword followed by an immediately invoked function expression (IIFE).
  84. What is a Promise in JavaScript?
    A Promise in JavaScript is an object that represents the eventual completion or failure of an asynchronous operation. Promises are used to handle asynchronous code, such as making HTTP requests or reading from a file. A Promise can be in one of three states: `pending`, `fulfilled`, or `rejected`. Once a Promise is fulfilled or rejected, it cannot be changed. Promises provide a way to handle asynchronous code in a more organized and cleaner way, and they are a fundamental part of modern JavaScript development.
  85. What is the difference between let and const in JavaScript?
    `let` and `const` are both used to declare variables in JavaScript, but there are some key differences between them. `let` variables can be reassigned to a new value, while `const` variables cannot be reassigned. `const` variables must be assigned a value when they are declared, while `let` variables can be declared without a value. In other words, `const` is used to declare variables that should never change, while `let` is used to declare variables that may change throughout the lifetime of the program.
  86. What is closure in JavaScript?
    A closure in JavaScript is a function that has access to variables in its outer scope even after the outer function has returned. Closures are created whenever a function is defined inside another function. The inner function has access to variables in the outer function's scope and can retain access to them even after the outer function has returned. Closures are often used to create private variables and to implement functional programming techniques.
  87. What is the difference between null and undefined in JavaScript?
    `undefined` is a value that indicates that a variable has been declared but has not been assigned a value. On the other hand, `null` is a value that represents the intentional absence of any object value. `null` is often used to indicate the absence of a value for an object property or to reset the value of a variable. In general, `undefined` is used when a variable is declared but has no value, while `null` is used to explicitly set a value to nothing.
  88. What is hoisting in JavaScript?
    Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of the current scope. In other words, regardless of where a variable or function is declared, it is treated as if it was declared at the top of the scope. This behavior makes it possible to use variables and functions before they are declared in the code, but it can also lead to unexpected behavior and bugs if the developer is not aware of hoisting.
  89. What is the difference between call and apply in JavaScript?
    Both `call` and `apply` are methods used to invoke a function and set its `this` value. The difference between the two is the way in which arguments are passed to the function. With `call`, arguments are passed as separate parameters, while with `apply`, arguments are passed as an array. Both methods allow you to invoke a function and set its `this` value, but `apply` is more flexible if you don't know the number of arguments that need to be passed to the function.
  90. What is the event loop in JavaScript?
    The event loop in JavaScript is a mechanism that allows the execution of code to be scheduled and handled asynchronously. It works by processing the message queue and executing any code that is waiting to be executed. The event loop continues to run until there are no more messages to be processed, at which point the JavaScript runtime will idle until there are more messages to be processed. The event loop is an important concept in JavaScript and is what allows you to perform multiple actions at the same time without blocking the main thread.
  91. What is the difference between let and var in JavaScript?
    `var` and `let` are both used to declare variables in JavaScript, but there are a few key differences between the two. Variables declared with `var` are function-scoped, which means that they are only accessible within the function in which they are declared. Variables declared with `let` are block-scoped, which means that they are only accessible within the block in which they are declared. Additionally, variables declared with `var` are hoisted to the top of the function, while variables declared with `let` are not. As a result, it is generally recommended to use `let` instead of `var` to avoid potential bugs and to make your code more readable.
  92. What is a Promise in JavaScript?
    A Promise in JavaScript is an object that represents the eventual completion or failure of an asynchronous operation. Promises provide a way to register callbacks to be executed when the operation is complete or if an error occurs. Promises are used to handle asynchronous operations in a more manageable and predictable way, and they play a central role in modern JavaScript programming.
  93. What is a generator function in JavaScript?
    A generator function in JavaScript is a special type of function that can be paused and resumed at any point, allowing it to return multiple values over time. Generator functions are defined using the `function*` syntax and can be paused using the `yield` keyword. Generator functions are often used to create iterators, which are objects that can be used to iterate over a collection of values.
  94. What is the difference between null and undefined in JavaScript?
    `undefined` is a value that indicates that a variable has been declared but has not been assigned a value. On the other hand, `null` is a value that represents the intentional absence of any object value. `null` is often used to indicate the absence of a value for an object property or to reset the value of a variable. In general, `undefined` is used when a variable is declared but has no value, while `null` is used to explicitly set a value to nothing.
  95. What is closure in JavaScript?
    A closure in JavaScript is a function that has access to variables in its outer scope even after the outer function has returned. Closures allow you to create functions with persistent data and to implement a variety of programming patterns, such as currying and partial application.
  96. What is the difference between a class and an object in JavaScript?
    In JavaScript, a class is a blueprint for creating objects, which defines a set of properties and methods that the objects will have. An object, on the other hand, is an instance of a class, created by calling the constructor function of the class. Classes in JavaScript can be thought of as a way to create custom data types, while objects are instances of those custom data types.
  97. What is hoisting in JavaScript?
    Hoisting in JavaScript is a behavior that occurs during the compilation phase where variables and function declarations are moved to the top of their respective scopes. This means that you can use a variable or call a function before it is declared, because the declaration is essentially moved to the top of the scope during compilation. However, this also means that you should be careful when using variables and functions before they are declared, as it can lead to unexpected results.
  98. What is the difference between for...in and for...of loops in JavaScript?
    In JavaScript, `for...in` and `for...of` are both looping constructs, but they behave differently. `for...in` is used to loop over the properties of an object, while `for...of` is used to loop over the values of an array-like object or any iterable object. `for...in` should generally be avoided when working with arrays, as it will also loop over any properties inherited from the object's prototype, while `for...of` only loops over the values of the array itself.
  99. What is an anonymous function in JavaScript?
    An anonymous function in JavaScript is a function that has no name and is not stored in a variable. Anonymous functions are often used as arguments to other functions or as immediate function expressions. Anonymous functions are useful for short, one-time use functions, such as event handlers or callbacks, and are a key part of functional programming in JavaScript.
  100. What is the difference between null and undefined in JavaScript?
    In JavaScript, `undefined` and `null` are both values that indicate the absence of a value. The difference between the two is that `undefined` means a variable has been declared but has not been assigned a value, while `null` is an assignment value that represents the intentional absence of any object value. In other words, `undefined` means that a variable has no value, while `null` means that a variable has been explicitly set to a value that represents no object.
  101. What is the purpose of using the 'use strict' directive in JavaScript?
    The `'use strict'` directive is a string literal that is placed at the top of a JavaScript file or within a function, and it is used to enable strict mode. Strict mode makes JavaScript a little more secure and a little more efficient by changing the behavior of certain features of the language, such as preventing the use of implicit global variables and making it easier to spot common coding mistakes, such as using an undeclared variable.
  102. What is an event bubbling in JavaScript?
    In JavaScript, event bubbling is a behavior that occurs when an event that is triggered on a child element also triggers the same event on its parent elements, until it reaches the root element of the document. For example, if you click on a button within a form, the `click` event will first be triggered on the button, then on the form, and then on the document. You can use event bubbling to handle events for multiple elements with a single event handler, by attaching the handler to a parent element that contains the elements you are interested in.
  103. What is the difference between .call() and .apply() in JavaScript?
    In JavaScript, `.call()` and `.apply()` are methods that allow you to call a function and set the `this` value within the function to a specific object. The difference between the two is that `.call()` takes arguments as separate parameters, while `.apply()` takes arguments as an array. Both methods can be used to invoke a function with a specific `this` value, and both are used to reuse functions in a different context, as well as to borrow methods from one object and use them in another.
  104. What is the difference between var, let, and const in JavaScript?
    In JavaScript, `var`, `let`, and `const` are used to declare variables. The difference between the three is in the scope and mutability of the variables they declare. `var` is function scoped, which means that a variable declared with `var` can be accessed within the function where it is declared and in any nested functions. `let` and `const` are block scoped, meaning that they can only be accessed within the block they are declared in. `let` allows the value of a variable to be reassigned, while `const` is used to declare a constant value that cannot be reassigned.

JavaScript Interview Questions for Senior Developers

Advanced topics like event loop internals, performance, architecture, testing, security, and Node.js/SSR patterns typically asked for senior roles.

  1. What is closure in JavaScript?
    A closure is a function object that has access to variables in its outer scope, even after the outer function has returned.
  2. What is hoisting in JavaScript?
    Hoisting is a mechanism in JavaScript where variables and function declarations are moved to the top of their respective scopes, regardless of where they are declared in the code.
  3. What is the difference between null and undefined in JavaScript?
    Both null and undefined represent a lack of value, but null is explicitly set by the programmer, while undefined means a variable has been declared but has not been assigned a value.
  4. What is the difference between synchronous and asynchronous code in JavaScript?
    Synchronous code is executed line by line, blocking further execution until it has completed. Asynchronous code allows multiple operations to be executed in parallel, without blocking further execution of the program.
  5. What is event bubbling and event capturing in JavaScript?
    Event bubbling means that an event is first captured and handled by the innermost element and then propagated to outer elements. Event capturing is the opposite process, where the event is first captured by the outermost element and then propagated to inner elements.
  6. What is the purpose of 'this' keyword in JavaScript?
    The 'this' keyword in JavaScript refers to the object that is executing the current function. Its value can be determined dynamically based on the context in which the function is being executed.
  7. What is the difference between let and var in JavaScript?
    The main difference between 'var' and 'let' is that 'var' has function scope, while 'let' has block scope. This means that a variable declared with 'var' is accessible within its entire enclosing function, while a variable declared with 'let' is only accessible within the block it is declared.
  8. What is a promise in JavaScript?
    A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. It allows you to register callbacks for the success or failure of an asynchronous operation, and provides a way to handle errors in asynchronous code.
  9. What is a WeakMap in JavaScript?
    A WeakMap in JavaScript is a collection of key-value pairs where the keys are weakly referenced. This means that the keys can be garbage collected if there are no other references to them. This makes WeakMap a good choice for storing data that is associated with an object but should not prevent the object from being garbage collected.
  10. What is a curry function in JavaScript?
    A curry function in JavaScript is a function that returns a new function that takes the remaining arguments until all of its arguments have been supplied. It allows for partial application of arguments, which can make it easier to reuse functions with similar arguments.
  11. What is the difference between map, filter, and reduce in JavaScript?
    The 'map' method creates a new array with the results of calling a provided function on every element in the original array. The 'filter' method creates a new array with elements that pass a provided test function. The 'reduce' method reduces an array to a single value by iteratively applying a provided reducer function to each element in the array.
  12. What is a pure function in JavaScript?
    A pure function in JavaScript is a function that given the same inputs, always returns the same outputs, and has no side-effects. It does not modify external state or cause any observable changes to the world outside the function.
  13. What is a higher-order function in JavaScript?
    A higher-order function in JavaScript is a function that takes one or more functions as arguments and/or returns a function as its result. Higher-order functions are used to abstract and encapsulate operations, making it easier to write reusable, composable code.
  14. What is a prototype in JavaScript?
    A prototype in JavaScript is an object that is associated with every function and object. The prototype can be used to add properties and methods to an object, allowing for inheritance and reuse of code.
  15. What is a generator function in JavaScript?
    A generator function in JavaScript is a special type of function that can be paused and resumed during execution. It allows you to produce a sequence of values over time, which can be used to handle asynchronous operations in a more readable and maintainable way.
  16. What is event delegation in JavaScript?
    Event delegation in JavaScript is a technique where you attach a single event handler to a parent element, rather than individual handlers for each child element. This allows you to handle events that occur in multiple elements more efficiently, as you only have to manage one event handler instead of multiple.
  17. What is the difference between call and apply in JavaScript?
    The 'call' and 'apply' methods in JavaScript allow you to call a function and set the 'this' keyword inside the function to a specified value. The main difference between 'call' and 'apply' is that 'call' takes a list of arguments separated by commas, while 'apply' takes an array of arguments.
  18. How would you implement a debounce function in JavaScript?
    A debounce function in JavaScript is used to delay the execution of a function until a specified amount of time has passed without it being called. To implement a debounce function, you can use setTimeout to delay the execution of the function, and clearTimeout to cancel the execution if the function is called again before the specified time has passed.
  19. How would you implement a throttle function in JavaScript?
    A throttle function in JavaScript is used to limit the number of times a function can be executed in a given time period. To implement a throttle function, you can use setTimeout to delay the execution of the function, and keep track of the last time the function was executed. If the function is called again before the specified time has passed, you can return without executing the function again.
  20. How would you implement a lazy load image function in JavaScript?
    To implement a lazy load image function in JavaScript, you can use the IntersectionObserver API to determine when an image is in the viewport. When an image is in the viewport, you can set the 'src' attribute to load the image. If the IntersectionObserver API is not available, you can use a scroll event listener to determine when an image is in the viewport and load the image accordingly.
  21. How would you implement an accordion component in JavaScript?
    To implement an accordion component in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the accordion, with headings and content sections. You can use CSS to style the accordion and hide the content sections. You can use JavaScript to toggle the display of the content sections when a heading is clicked.
  22. How would you implement a modal component in JavaScript?
    To implement a modal component in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the modal, with a container and a content area. You can use CSS to style the modal and position it in the center of the screen. You can use JavaScript to open and close the modal, and add an overlay to prevent interaction with the background.
  23. How would you implement a tab component in JavaScript?
    To implement a tab component in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the tab, with tab headings and content sections. You can use CSS to style the tab and hide the content sections. You can use JavaScript to switch the display of the content sections when a tab heading is clicked.
  24. How would you implement a carousel component in JavaScript?
    To implement a carousel component in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the carousel, with slides and navigation buttons. You can use CSS to style the carousel and position the slides. You can use JavaScript to switch the display of the slides and update the navigation buttons when a slide changes.
  25. How would you implement a responsive navigation menu in JavaScript?
    To implement a responsive navigation menu in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the navigation menu, with menu items and a toggle button. You can use CSS to style the navigation menu and hide the menu items on smaller screens. You can use JavaScript to toggle the display of the menu items when the toggle button is clicked.
  26. How would you implement a search feature in JavaScript?
    To implement a search feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML input for the search query, and use JavaScript to filter the results based on the query. You can use CSS to style the search input and results.
  27. How would you implement a sorting function in JavaScript?
    To implement a sorting function in JavaScript, you can use the Array.sort() method. You can pass a compare function to the sort() method to specify the sorting order. The compare function should return a negative value if the first argument should be sorted before the second argument, a positive value if the first argument should be sorted after the second argument, and 0 if the arguments are equal.
  28. How would you implement a pagination feature in JavaScript?
    To implement a pagination feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the pagination, with page numbers and navigation buttons. You can use CSS to style the pagination. You can use JavaScript to update the display of the pages and navigation buttons when a page changes.
  29. How would you implement a sticky header in JavaScript?
    To implement a sticky header in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the header. You can use CSS to style the header and set its position to 'fixed' when it reaches the top of the viewport. You can use JavaScript to calculate the distance between the top of the header and the top of the viewport, and update the header's position accordingly.
  30. How would you implement an image lightbox in JavaScript?
    To implement an image lightbox in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the lightbox, with an overlay, a close button, and an image. You can use CSS to style the lightbox and position the overlay and close button. You can use JavaScript to open and close the lightbox and update the image when a different image is clicked.
  31. How would you implement a to-do list application in JavaScript?
    To implement a to-do list application in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the to-do list, with a form for adding tasks and a list of tasks. You can use CSS to style the to-do list. You can use JavaScript to add and remove tasks, update the display of the list, and store the tasks in local storage so that they persist between page reloads.
  32. How would you implement an autocomplete feature in JavaScript?
    To implement an autocomplete feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML input for the autocomplete. You can use CSS to style the autocomplete and suggestions. You can use JavaScript to fetch suggestions based on the input value and update the display of the suggestions.
  33. How would you implement a password strength meter in JavaScript?
    To implement a password strength meter in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the password strength meter, with a password input and a meter. You can use CSS to style the password strength meter. You can use JavaScript to calculate the strength of the password based on various factors such as length, complexity, and use of different characters, and update the display of the meter accordingly.
  34. How would you implement an infinite scrolling feature in JavaScript?
    To implement an infinite scrolling feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the content, with a list of items. You can use CSS to style the content and list of items. You can use JavaScript to fetch new items when the user scrolls to the bottom of the page and update the display of the list accordingly.
  35. How would you implement a progress bar in JavaScript?
    To implement a progress bar in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the progress bar, with a container and a bar. You can use CSS to style the progress bar. You can use JavaScript to update the width of the bar based on the progress and update the display of the bar accordingly.
  36. How would you implement a file upload feature in JavaScript?
    To implement a file upload feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML form for the file upload, with a file input and a submit button. You can use CSS to style the form. You can use JavaScript to process the file and send it to the server using XHR or Fetch API. You can also handle any error and display the progress of the upload.
  37. How would you implement a drag and drop feature in JavaScript?
    To implement a drag and drop feature in JavaScript, you can use the Drag and Drop API. You can attach event listeners to the draggable elements, such as `dragstart`, `drag`, `dragend`, etc. You can also handle the drop event on the drop target to determine what to do with the dropped elements. You can use CSS to style the draggable elements and the drop target.
  38. How would you implement a modal in JavaScript?
    To implement a modal in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the modal, with an overlay, a modal container, and a close button. You can use CSS to style the modal and position the overlay and close button. You can use JavaScript to open and close the modal and update the content of the modal when necessary.
  39. How would you implement a slider in JavaScript?
    To implement a slider in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the slider, with a container, slides, and navigation buttons. You can use CSS to style the slider and position the slides. You can use JavaScript to change the displayed slide, handle user interactions, and update the display of the slider accordingly.
  40. How would you implement a tooltip in JavaScript?
    To implement a tooltip in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the tooltip, with a trigger element and a tooltip container. You can use CSS to style the tooltip and position the container relative to the trigger. You can use JavaScript to show and hide the tooltip based on the interaction with the trigger and update the content of the tooltip when necessary.
  41. How would you implement a rating system in JavaScript?
    To implement a rating system in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the rating system, with a container and rating elements. You can use CSS to style the rating system and position the elements. You can use JavaScript to handle user interactions, update the rating, and send the data to the server if necessary.
  42. How would you implement an autocomplete feature in JavaScript?
    To implement an autocomplete feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML input for the autocomplete, and attach an event listener to it, such as `input`, to trigger the autocomplete. You can use JavaScript to process the input, fetch the data from the server, and filter the results based on the input. You can display the results in a dropdown or a list, and handle user interactions, such as selecting a result, to update the input and close the dropdown or list.
  43. How would you implement an image carousel in JavaScript?
    To implement an image carousel in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the carousel, with a container, slides, and navigation buttons. You can use CSS to style the carousel and position the slides. You can use JavaScript to change the displayed slide, handle user interactions, and update the display of the carousel accordingly.
  44. How would you implement a text editor in JavaScript?
    To implement a text editor in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the editor, with a container, a toolbar, and a content area. You can use CSS to style the editor and position the elements. You can use JavaScript to handle user interactions, such as selecting a tool or formatting the text, and update the content of the editor accordingly.
  45. How would you implement a to-do list in JavaScript?
    To implement a to-do list in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the list, with a container, items, and input for adding new items. You can use CSS to style the list and position the elements. You can use JavaScript to handle user interactions, such as adding or removing items, and update the display of the list accordingly.
  46. How would you implement a responsive navigation in JavaScript?
    To implement a responsive navigation in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the navigation, with a container, items, and a toggle button for the responsive version. You can use CSS to style the navigation, position the items, and hide or show the toggle button based on the viewport size. You can use JavaScript to handle user interactions, such as toggling the navigation, and update the display of the navigation accordingly.
  47. How would you implement a form validation in JavaScript?
    To implement a form validation in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML form with inputs and a submit button. You can use CSS to style the form and the inputs. You can use JavaScript to validate the form inputs on the client-side, by checking the values and the types of the inputs.
  48. How would you implement a search feature in JavaScript?
    To implement a search feature in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML input for the search and a container for the results. You can use CSS to style the search and the results. You can use JavaScript to process the input, fetch the data from the server, filter the results based on the input, and display the results in the container. You can also handle user interactions, such as clicking on a result, and update the display accordingly.
  49. How would you implement a sort feature in JavaScript?
    To implement a sort feature in JavaScript, you can use JavaScript to sort an array of data. You can use the built-in `Array.sort()` method, or you can write your own sorting algorithm, such as bubble sort or insertion sort. You can then update the display of the sorted data, such as a table or a list, to reflect the changes.
  50. How would you implement a filter feature in JavaScript?
    To implement a filter feature in JavaScript, you can use JavaScript to filter an array of data. You can use the built-in `Array.filter()` method, or you can write your own filtering function, based on the conditions you want to apply. You can then update the display of the filtered data, such as a table or a list, to reflect the changes.
  51. How would you implement a pagination feature in JavaScript?
    To implement a pagination feature in JavaScript, you can use JavaScript to divide an array of data into pages, based on the desired number of items per page. You can then update the display of the data, such as a table or a list, to show only the items for the current page, and add navigation buttons or links to switch between pages.
  52. How would you implement a progress bar in JavaScript?
    To implement a progress bar in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the progress bar, with a container, a bar, and a label. You can use CSS to style the progress bar and position the elements. You can use JavaScript to update the value and width of the bar, based on the progress, and update the label with the value and the percentage.
  53. How would you implement a modal in JavaScript?
    To implement a modal in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the modal, with a container, a header, a body, and a footer. You can use CSS to style the modal and position the elements. You can use JavaScript to handle user interactions, such as opening and closing the modal, and update the display of the modal accordingly.
  54. How would you implement a carousel in JavaScript?
    To implement a carousel in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the carousel, with a container, slides, and navigation buttons. You can use CSS to style the carousel and position the elements. You can use JavaScript to handle user interactions, such as clicking on the navigation buttons, and update the display of the slides accordingly, such as moving them left or right.
  55. How would you implement a slider in JavaScript?
    To implement a slider in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the slider, with a container, a track, handles, and labels. You can use CSS to style the slider and position the elements. You can use JavaScript to handle user interactions, such as dragging the handles, and update the display of the slider accordingly, such as moving the handles and updating the labels with the values.
  56. How would you implement a drag and drop feature in JavaScript?
    To implement a drag and drop feature in JavaScript, you can use JavaScript to handle mouse and touch events, such as `mousedown`, `mousemove`, and `mouseup`, or `touchstart`, `touchmove`, and `touchend`. You can track the position of the mouse or touch, and update the position of the dragged element accordingly. You can also handle other aspects of drag and drop, such as constraints, snapping, and drop targets, by adding more code to your implementation.
  57. How would you implement a zoom feature in JavaScript?
    To implement a zoom feature in JavaScript, you can use JavaScript to handle user interactions, such as mouse wheel, pinch, or double-click, and update the scale of an element, such as an image, accordingly. You can use CSS `transform` property, such as `scale`, to apply the zoom, and use JavaScript to calculate and update the `scale` value based on the interaction. You can also handle other aspects of zoom, such as limits, centering, or animation, by adding more code to your implementation.
  58. How would you implement an autocomplete feature in JavaScript?
    To implement an autocomplete feature in JavaScript, you can use JavaScript to fetch data from a server, such as a list of suggestions, and process the input from an HTML input element. You can then filter the data based on the input, and display the suggestions in a dropdown or a list, under the input. You can also handle user interactions, such as selecting a suggestion, and update the input accordingly.
  59. How would you implement a tooltip in JavaScript?
    To implement a tooltip in JavaScript, you can use a combination of HTML, CSS, and JavaScript. You can create an HTML structure for the tooltip, with a trigger element and a tooltip container. You can use CSS to style the tooltip and position the container relative to the trigger. You can use JavaScript to show and hide the tooltip based on the interaction with the trigger and update the content of the tooltip when necessary.
  60. What is closure in JavaScript and how it can be useful?
    A closure in JavaScript is a function that has access to its outer variables even after its outer function has returned. This allows the closure to 'remember' the state of the outer function and use it even after it has completed its execution. Closures can be useful in various scenarios such as implementing private variables, creating utility functions that have access to outer variables, and preserving the state of a function across multiple invocations.
  61. What is a higher-order function in JavaScript and how it can be useful?
    A higher-order function in JavaScript is a function that takes one or more functions as arguments and/or returns a function as its result. Higher-order functions can be useful in many ways, such as abstracting common logic into reusable functions, simplifying the code by reducing the amount of duplicated code, and enabling functional programming patterns such as currying and partial application.
  62. What is the difference between `let` and `var` in JavaScript?
    `var` is function scoped in JavaScript, which means it is accessible within the function it was declared, as well as its outer scopes. `let` and `const` are block scoped, which means they are only accessible within the block they were declared and any nested blocks. Another difference between `var` and `let` is that `var` is hoisted to the top of its scope, meaning it is accessible before it was declared, while `let` is not hoisted and is only accessible after it was declared.
  63. What is the difference between `null` and `undefined` in JavaScript?
    `undefined` is a value in JavaScript that represents the absence of a value. It is automatically assigned to variables that are declared but not assigned a value. `null` is a value in JavaScript that represents the intentional absence of any object value. It can be assigned to a variable to represent the deliberate absence of any value.
  64. What is the difference between `==` and `===` in JavaScript?
    `==` is the equality operator in JavaScript and performs type coercion, which means it converts the operands to the same type before comparing them. `===` is the strict equality operator in JavaScript and does not perform type coercion, which means it only returns `true` if the operands have the same type and value. It is considered best practice to use `===` instead of `==` for comparison in JavaScript.
  65. What is event bubbling and event capturing in JavaScript?
    Event bubbling and event capturing are two ways of propagating events in the DOM (Document Object Model) tree. Event bubbling is the propagation of an event from the target element to its parent elements, starting from the target element and going up the DOM tree. Event capturing is the propagation of an event from the outermost element to the target element, starting from the outermost element and going down the DOM tree. JavaScript provides methods such as `addEventListener` and `removeEventListener.
  66. What is the difference between an array and an object in JavaScript?
    An array in JavaScript is an ordered list of values, accessed by index. An object in JavaScript is a collection of key-value pairs, where the keys are used to access the values. Arrays have built-in methods for iteration and manipulation, while objects do not have as many built-in methods, but can still be manipulated using for...in loops and Object.keys() and Object.values() methods.
  67. What is hoisting in JavaScript and how does it work?
    Hoisting in JavaScript is the behavior of moving variable and function declarations to the top of their respective scopes at runtime. This means that variables declared with `var` and functions declared with the `function` keyword are accessible before they are declared in their respective scopes. However, only the declarations are hoisted, not the assignments, so if you try to access a variable before it is declared, it will return `undefined`.
  68. What is the difference between synchronous and asynchronous code in JavaScript?
    Synchronous code in JavaScript executes one line of code after the other, blocking the execution of any further code until the current line of code has completed. Asynchronous code, on the other hand, allows multiple lines of code to execute simultaneously, without blocking the execution of other code. Asynchronous code is often used in JavaScript to handle events, such as user interactions and network requests, without freezing the user interface.
  69. What is the event loop in JavaScript and how does it work?
    The event loop in JavaScript is a mechanism that allows the execution of code to be scheduled asynchronously. The event loop works by continually checking the message queue for new messages (also known as tasks), and executing the code for each message in a single thread, one at a time. When there are no more messages in the queue, the event loop will wait until a new message is added, and then repeat the process.
  70. What is the difference between null and undefined in JavaScript?
    `undefined` in JavaScript means that a variable has been declared, but has not been assigned a value. `null` is a value that represents the intentional absence of any object value, and can be assigned to a variable to indicate that it has no value.
  71. What is the difference between `==` and `===` in JavaScript?
    `==` is the equality operator in JavaScript, and performs type coercion, meaning that it converts the operands to the same type before comparing them. `===` is the strict equality operator in JavaScript, and does not perform type coercion, meaning that it only returns `true` if the operands have the same type and value.
  72. What is closure in JavaScript and how does it work?
    A closure in JavaScript is a function that has access to variables in its outer scope, even after the outer function has returned. A closure is created when a function is declared inside another function, and the inner function has access to the variables in the outer function's scope. The closure can be returned as a function object, and can retain access to the variables in its outer scope, even after the outer function has returned.
  73. What is a JavaScript Promise and how does it work?
    A Promise in JavaScript is an object that represents the eventual completion or failure of an asynchronous operation. A Promise is created using the `Promise` constructor, and has a `then` method, which is called when the Promise is resolved, and a `catch` method, which is called when the Promise is rejected. The `then` method takes two arguments, a callback function to be executed when the Promise is resolved, and a callback function to be executed when the Promise is rejected.
  74. What is a JavaScript generator and how does it work?
    A generator in JavaScript is a function that can be exited and later re-entered, allowing the function to preserve its state across multiple calls. Generators are declared using the `function*` syntax, and can be executed using the `next` method. The `next` method returns an object with two properties: `value`, which is the value returned by the generator, and `done`, which is a boolean value indicating whether the generator has completed or not.
  75. What is the difference between call and apply in JavaScript?
    `call` and `apply` in JavaScript are both methods that can be used to invoke a function with a specified `this` value and arguments. The difference between `call` and `apply` is in the way the arguments are passed to the function: `call` takes a list of arguments separated by commas, while `apply` takes an array of arguments.
  76. What is the difference between a class and an object in JavaScript?
    A class in JavaScript is a blueprint for creating objects, and can be used to define properties and methods for those objects. An object is an instance of a class, and has properties and methods defined by its class. Classes in JavaScript were introduced in ECMAScript 6, and provide a more classical object-oriented programming syntax for defining objects in JavaScript.
  77. What is the difference between let and var in JavaScript?
    `var` is a function scoped variable in JavaScript, while `let` is a block scoped variable. This means that a variable declared with `var` can be accessed within the entire function it is declared in, while a variable declared with `let` can only be accessed within the block it is declared in.
  78. What is closure in JavaScript and how does it work?
    A closure in JavaScript is an inner function that has access to the outer (enclosing) function's variables and parameters, as well as to its own variables and parameters. A closure is created when the inner function is returned from the outer function, allowing it to retain access to the variables and parameters in the outer function's scope even after the outer function has completed executing.
  79. What is the difference between function declaration and function expression in JavaScript?
    A function declaration in JavaScript is a function that is defined using the `function` keyword, followed by the function name, and a list of parameters enclosed in parentheses. Function declarations are hoisted to the top of their scope, meaning they can be used before they are declared. A function expression, on the other hand, is a function that is defined as a part of a larger expression, such as when it is assigned to a variable. Function expressions are not hoisted and can only be used after they are defined.
  80. What is the difference between let and var in JavaScript?
    `var` and `let` are both used to declare variables in JavaScript, but they have different scoping rules. Variables declared with `var` have function scope, meaning they are accessible within the entire function in which they are declared, as well as any nested functions. Variables declared with `let` have block scope, meaning they are only accessible within the block in which they are declared and any nested blocks.
  81. What is the difference between spread operator and rest parameter in JavaScript?
    The spread operator in JavaScript is used to spread the elements of an array or object into a new array or object, respectively. The rest parameter, on the other hand, is used to represent an indefinite number of arguments as an array in a function declaration. The spread operator is used to spread elements from one array or object into another, while the rest parameter is used to capture multiple arguments into an array within a function.
  82. What is event bubbling and event capturing in JavaScript?
    Event bubbling and event capturing are two ways of propagating events in the DOM (Document Object Model). Event bubbling is the default behavior in which events are first captured by the innermost element and then propagate outward to the outer elements. Event capturing is the reverse of event bubbling, where events are first captured by the outermost element and then propagate inward to the inner elements. Developers can choose between event bubbling and event capturing by using the `useCapture` argument in the `addEventListener` method.
  83. What is the difference between deep and shallow copying in JavaScript?
    Deep copying in JavaScript is the process of creating a new object that has the same properties and values as the original object, but with a completely separate reference. This means that changes made to the new object will not affect the original object. Shallow copying, on the other hand, is the process of creating a new object that has the same properties and values as the original object.
  84. What is a Promise in JavaScript and how does it work?
    A Promise in JavaScript is an object that represents the eventual completion or failure of an asynchronous operation. Promises provide a way to register callbacks to be executed when the asynchronous operation is complete or if it fails. Promises can be in one of three states: pending, resolved, or rejected. A Promise is considered resolved if the asynchronous operation is successful, and rejected if the operation fails. Promises provide a cleaner and more manageable way of handling asynchronous operations in JavaScript.
  85. What is an async function in JavaScript and how does it work?
    An async function in JavaScript is a function declared with the `async` keyword that returns a Promise. Async functions make it easier to work with asynchronous operations in JavaScript by allowing the use of `await` within the function. The `await` keyword is used to wait for a Promise to resolve or reject before moving on to the next line of code. This allows for cleaner and more manageable code, as it avoids the callback hell that can occur when working with Promises and other asynchronous operations.
  86. What is the difference between undefined and null in JavaScript?
    In JavaScript, `undefined` and `null` are both used to indicate that a value does not exist. However, they are different in the sense that `undefined` means a variable has been declared but has not been assigned a value, while `null` is a value that can be assigned to a variable to indicate that it does not have a value. In other words, `undefined` represents the absence of a value, while `null` is a value that represents the absence of a value.
  87. What is a WeakMap in JavaScript and how does it differ from a Map?
    A WeakMap in JavaScript is a collection of key-value pairs where the keys are weakly referenced, meaning that the keys can be garbage collected if there are no other references to them. This makes WeakMaps useful for situations where it is necessary to associate data with an object without preventing that object from being garbage collected. WeakMaps differ from Maps in that Maps have strong references to their keys, meaning that the keys cannot be garbage collected and will continue to take up memory even if there are no other references to them.
  88. What is the difference between == and === in JavaScript?
    In JavaScript, `==` and `===` are both used to compare values, but they have different rules for type coercion. The `==` operator performs type coercion if necessary, meaning that it will attempt to convert the operands to the same type before making the comparison. The `===` operator, on the other hand, does not perform type coercion and only returns true if the operands have the same type and value. As a result, `===` is considered a more strict equality operator, as it does not perform type coercion, while `==` is considered a more lenient equality operator that may perform type coercion.
  89. What is the difference between const and let in JavaScript?
  90. You are building an e-commerce website and need to implement a shopping cart for customers. How would you approach this problem in JavaScript?
    I would create an object or an array to store the items in the shopping cart, along with their properties such as name, quantity, and price. I would then use JavaScript functions to add items to the cart, remove items from the cart, and calculate the total cost of the items in the cart. To persist the shopping cart data across pages, I would consider using local storage or a server-side solution such as a cookie or a database.
  91. You have been asked to optimize a slow running JavaScript application. How would you approach this problem?
    I would start by profiling the application using a tool such as the Chrome DevTools to identify the slow running parts of the code. Then, I would look for any opportunities for code optimization, such as reducing the number of function calls, removing unnecessary code, and improving algorithms. I would also consider implementing lazy loading for images and other resources, and compressing and minifying the JavaScript code to reduce file size and improve load times. Finally, I would test the optimizations to ensure that they have improved the performance of the application.
  92. You are building a web application that needs to update content on the page in real-time. How would you approach this problem in JavaScript?
    I would use WebSockets or Server-Sent Events to establish a persistent connection between the client and the server, allowing for real-time communication between the two. I would use JavaScript to subscribe to the updates from the server and update the content on the page as necessary. I would also consider implementing error handling and fallback mechanisms in case the real-time connection is lost, such as periodically polling the server for updates.
  93. You have been asked to implement a search feature for a large data set in a JavaScript application. How would you approach this problem?
    I would start by indexing the data set to make searching more efficient. I would then create a search function that takes a query string as input and returns the matching results. To further optimize the search, I would consider using a search algorithm such as full-text search or fuzzy search, and I would implement pagination to limit the number of results returned for each search to improve performance.
Tip: Practice aloud. Explaining an answer reveals gaps faster than silent reading.