While reading through a JavaScript codebase, you see a function that is returned from another function and retains access to its lexical scope, even after the outer function has finished execution. What is this pattern called?

  • Callback
  • Promise
  • IIFE (Immediately Invoked Function Expression)
  • Higher-order Function
This pattern is known as a "Higher-order Function." In JavaScript, higher-order functions are functions that can take other functions as arguments or return them. This enables functions to maintain access to their lexical scope, creating closures.

What is the purpose of the status property in an HTTP response?

  • To specify the content type
  • To indicate the HTTP status
  • To define the response data
  • To specify the request method
The status property in an HTTP response is used to indicate the HTTP status code for the response. HTTP status codes provide information about the outcome of an HTTP request, such as whether it was successful (e.g., status code 200 for OK) or encountered an error (e.g., status code 404 for Not Found). It helps the client understand the result of the request.

You are debugging a JavaScript application and encounter a ReferenceError at runtime, despite a function being defined in the code. What could be the possible reason if the function was defined using a function expression?

  • The function is declared within a block scope.
  • The function was defined in strict mode.
  • The function was hoisted to the top of the code.
  • The function was defined inside another function.
When a function is defined using a function expression and declared within a block scope, it may not be accessible outside of that block, resulting in a ReferenceError. This is because the function's scope is limited to the block where it's defined.

When dealing with callbacks, the first argument is traditionally

  • Callback
  • Error
  • Function
  • Result
When dealing with callbacks, the first argument is traditionally reserved for Error. This convention allows developers to handle errors gracefully when executing asynchronous operations. It helps in proper error handling and prevents unexpected crashes in your code.

How does the browser determine the path along which the event propagates?

  • Bubbling phase and capturing phase both follow the DOM hierarchy from the target element to the document root.
  • It is determined by the order in which event listeners were added.
  • The browser chooses a random path for event propagation.
  • It follows the DOM hierarchy from the document root to the target element.
The browser determines the path of event propagation based on the DOM hierarchy. Events can propagate in two phases: capturing and bubbling. In the capturing phase, events travel from the document root to the target element. In the bubbling phase, events travel from the target element back up to the document root. This order ensures that ancestors and descendants of the target element have an opportunity to handle the event.

In JavaScript, a common way to utilize prototypes is by assigning a(n) _________ to an object's prototype property.

  • Array
  • Object Literal
  • Constructor Function
  • Prototype Object
In JavaScript, prototypes are often utilized by assigning a constructor function to an object's prototype property. This constructor function serves as a blueprint for creating objects with shared properties and methods. The prototype object associated with this constructor contains those shared properties and methods.

What is a practical use of closures in JavaScript?

  • Encapsulation
  • Inheritance
  • Code Reusability
  • Memory Management
Closures in JavaScript are often used for encapsulation. They allow variables and functions to be "enclosed" within a function, preventing them from polluting the global scope and providing a level of data privacy. This enables code organization, reduces naming conflicts, and promotes modularity in your programs. Closures are a key feature in achieving encapsulation in JavaScript.

In Internet Explorer, instead of addEventListener, the _________ method is used to attach event listeners.

  • attachEvent()
  • registerListener()
  • listenForEvent()
  • addListener()
In Internet Explorer, the attachEvent() method is used to attach event listeners. This method is specific to Internet Explorer and serves a similar purpose to addEventListener() in other browsers, allowing you to respond to events such as clicks and keypresses.

If you’re using arrow functions to define methods inside a class, those methods will not have access to the class’s instance without using _________.

  • super()
  • this
  • prototype
  • new
If you use arrow functions to define methods within a class, they will not have their own this context. Instead, they inherit the this context from the surrounding scope. To access the class instance within an arrow function, you need to use this. Hence, the correct option is this.

How can you prematurely terminate a while loop?

  • break statement
  • return statement
  • continue statement
  • exit statement
You can prematurely terminate a while loop using the break statement. When a break statement is encountered within the loop, it exits the loop prematurely, regardless of whether the loop's condition is still true. This is useful for stopping the loop when a specific condition is met.