What is the primary difference between for...in and for...of loops?

  • The order of iteration and syntax
  • The type of objects they iterate over
  • They are interchangeable
  • The number of iterations they perform
The primary difference between for...in and for...of loops is in their order of iteration and syntax. For...in is used for iterating over object properties, while for...of is used for iterating over the values of iterable objects, like arrays and strings, and maintains the order.

Which of the following is a way to create a singleton object in JavaScript?

  • Using the Factory Pattern
  • Using the Prototype Pattern
  • Using the Singleton Pattern
  • Using the Module Pattern
A singleton object in JavaScript is typically created using the Module Pattern. The Module Pattern allows you to encapsulate code and create a single instance of an object, ensuring that only one instance of the object exists throughout your application. The other options (Factory, Prototype, and Singleton patterns) serve different purposes.

What was the main reason for JavaScript's creation at Netscape?

  • To compete with Microsoft's JScript
  • To create a replacement for HTML
  • To enhance interactivity in web browsers
  • To provide a server-side scripting language
JavaScript was primarily created to enhance interactivity in web browsers. It allowed developers to manipulate web page elements and respond to user actions, making websites more dynamic. While JavaScript later found uses on the server-side as well, its initial purpose was to enhance the client-side user experience.

In JavaScript, the "this" keyword inside an arrow function is defined by its _________ context.

  • Global
  • Lexical
  • Local
  • Execution
In JavaScript, the "this" keyword inside an arrow function is defined by its lexical context. Unlike regular functions, arrow functions do not have their own "this" binding, so they inherit the "this" value from the surrounding code block, which is determined by the lexical scope.

Which method can be used to stop the event from propagating in the capturing or bubbling phase?

  • event.stopPropagation()
  • event.preventDefault()
  • event.stopPropagationPhase()
  • event.cancelPropagation()
The event.stopPropagation() method is used to stop the event from propagating further in the DOM tree. It prevents both capturing and bubbling phases, ensuring that the event doesn't trigger any other event listeners on the same element or its ancestors. This can be helpful in controlling event flow.

A ________ object represents a group of response headers, allowing you to query them and take different actions depending on the results.

  • a) XMLHttpRequest Object
  • b) FetchEvent Object
  • c) Headers Object
  • d) Worker Object
A Headers object represents a group of response headers. It allows you to query and manipulate these headers, which can be crucial when you need to inspect or modify headers in HTTP responses. You can use it for tasks like checking for specific headers or adding custom headers to a request.

In JavaScript, variables declared with the var keyword have _________ scope.

  • Block
  • Local
  • Global
  • Function
In JavaScript, variables declared with the var keyword have global scope. This means they are accessible throughout the entire function or script, regardless of where they are declared within that function or script. It's important to be cautious when using var to avoid unintended global variable declarations.

In a code review, you spot the line const arr = [10, 20, 30]; followed by arr = [40, 50, 60];. What will be the outcome when this code is executed?

  • It will result in an error.
  • The arr variable will now reference [40, 50, 60].
  • The original array [10, 20, 30] will be modified.
  • It will create a new variable arr with [40, 50, 60].
The code will result in an error. When you declare a variable using const, it cannot be reassigned to a different value or reference. Attempting to reassign arr to [40, 50, 60] will throw a "TypeError" because it violates the immutability of const variables.

If you are developing a real-time application where any blocking of the event loop can lead to critical issues, how might you implement a "for" loop to process an array of data without introducing any blockage?

  • Use for...of loop
  • Use setInterval to break up iterations
  • Use for...in loop
  • Use a synchronous for loop with a delay
In a real-time application, using a for...of loop is the recommended approach because it doesn't block the event loop. It iterates through the array without causing delays. Using setInterval is not suitable for processing an array as it introduces an asynchronous behavior. for...in loop is used for object iteration, and a synchronous for loop with a delay would still block the event loop.

What is the drawback of using "inheritance" through the prototype chain?

  • Limited support for multiple inheritance
  • Increased memory consumption
  • Difficulty in understanding and debugging
  • Inability to create encapsulated objects
One drawback of using "inheritance" through the prototype chain in JavaScript is limited support for multiple inheritance. JavaScript does not support multiple inheritance directly, meaning an object cannot inherit from multiple prototypes simultaneously. This limitation can lead to complex workarounds or potential conflicts when trying to inherit from multiple sources. While it's possible to implement multiple inheritance in JavaScript using mixins or other patterns, it's not as straightforward as in some other programming languages.