How can you create a new Promise?

  • Using the new Promise() constructor
  • Using the async/await syntax
  • By directly returning a value
  • Using the try/catch block
You can create a new Promise in JavaScript using the new Promise() constructor. This constructor takes a single argument, a function, which in turn takes two parameters, resolve and reject. Inside this function, you define the asynchronous operation, and when it's complete, you call resolve with the result if it's successful or reject if there's an error.

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.

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.

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.

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.

What does the responseType property of an XMLHttpRequest object determine?

  • The data type of the request
  • The request method
  • The URL of the request
  • The timeout for the request
The responseType property of an XMLHttpRequest object determines the expected data type of the response. It allows you to specify whether you expect the response to be in text, JSON, XML, or other formats. Setting this property correctly is important for parsing and handling the response data properly in your JavaScript code.

What is "explicit binding" in JavaScript with regard to the "this" keyword?

  • Explicit binding refers to manually setting the "this" value for a function using methods like .bind(), .call(), or .apply().
  • Explicit binding is when "this" is automatically determined by the JavaScript engine.
  • Explicit binding involves using the "this" keyword without any special methods.
  • Explicit binding is only applicable to arrow functions.
In JavaScript, "explicit binding" refers to the manual setting of the "this" value for a function. This is done using methods like .bind(), .call(), or .apply(), allowing you to explicitly specify the object that should be treated as "this" within the function. This is especially useful in cases where the default "this" behavior is not what you need.

How does the “for...of” loop differ from the traditional "for" loop?

  • It cannot iterate over arrays.
  • It can only be used for asynchronous operations.
  • It is specifically designed for iterating over arrays and iterable objects.
  • It has better performance when iterating over objects.
The "for...of" loop is used for iterating over arrays and iterable objects, providing a simplified syntax compared to the traditional "for" loop. It's particularly useful when dealing with collections of data, offering cleaner code for iteration. Traditional "for" loops are more versatile but require more verbose code for array iteration.

You're developing a game and need to create multiple instances of a player object, each with slightly different properties. Which object creation pattern might be most appropriate to use?

  • Prototype Pattern
  • Singleton Pattern
  • Factory Method Pattern
  • Abstract Factory Pattern
In game development, you would often use the Factory Method Pattern. This pattern provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. In the context of a game, you can have a PlayerFactory that creates different types of players with varying properties while maintaining a consistent creation interface.

How can Cross-Origin Resource Sharing (CORS) issues be handled when using AJAX?

  • Disable CORS in the browser
  • Use JSONP for requests
  • Send requests with credentials
  • Use the 'Access-Control-Allow-Origin' header
Cross-Origin Resource Sharing (CORS) issues when using AJAX can be handled by configuring the server to include the 'Access-Control-Allow-Origin' header in its response. This header specifies which origins are allowed to access the server's resources. You can set it to '*' to allow any origin or specify specific origins. Additionally, if you need to include credentials (e.g., cookies) in your requests, you should set the 'withCredentials' property to true in your AJAX request.

Using the new keyword invokes a _______ that creates a new object.

  • constructor
  • function
  • class
  • prototype
Using the new keyword in JavaScript invokes a constructor function that creates a new object. This object is then initialized with properties and methods defined within the constructor function. The constructor function serves as a blueprint for creating objects.

How do arrow functions handle arguments in comparison to traditional functions?

  • Arrow functions don't have access to arguments.
  • Arrow functions handle arguments the same way as traditional functions.
  • Arrow functions receive arguments as a separate object.
  • Arrow functions receive arguments as individual parameters.
Arrow functions do not have access to the arguments object like traditional functions do. Instead, they handle arguments the same way as regular functions if you explicitly define parameters. Each parameter corresponds to an argument passed to the function, allowing you to access them directly within the function body. The absence of the arguments object can be a limitation in certain scenarios.