How can a "for" loop be used to iterate through the properties of an object?

  • By using for...of loop
  • By using for...in loop
  • By using forEach method
  • By using while loop
To iterate through the properties of an object, you should use a for...in loop. This loop iterates over the enumerable properties of an object and allows you to access each property name (key). The for...of loop is used for iterating over iterable objects like arrays. The forEach method is specifically used for arrays, and while loops are generally used for repetitive tasks with a condition.

In order to make an object iterable with a for...of loop, you need to define its _______ method.

  • loop
  • forEach
  • Symbol.iterator
  • Object.iterable
To make an object iterable with a for...of loop, you need to define its Symbol.iterator method. This method should return an iterator object, which must have a next method that provides values one at a time. This enables you to use a for...of loop to iterate over the object's elements.

What is the result of the comparison operator === if the operands are of different types?

  • FALSE
  • TRUE
  • Undefined
  • Error
The comparison operator === (strict equality) in JavaScript returns true if the operands are of different types and have the same value. JavaScript performs type coercion with ==, but === strictly checks both value and type.

You're building a weather application and you're using the Fetch API to request weather data from a third-party API. However, you realize that the application does not properly handle when the API is down. How would you handle this to inform the user?

  • Implement a try-catch block to catch network errors and display a user-friendly message.
  • Use the finally block to handle any errors and show an alert to the user.
  • Utilize the window.onerror event to detect API failures and log them.
  • Set up a timer to periodically check the API status and notify the user if it's down.
To handle API failures and inform the user, you should implement a try-catch block around the fetch request. This allows you to catch network errors, like when the API is down, and then display a user-friendly message or take appropriate action. The other options are not recommended for handling API failures effectively.

How does JavaScript’s prototype inheritance differ from classical inheritance models?

  • JavaScript uses prototype-based inheritance, allowing objects to inherit directly from other objects.
  • JavaScript's prototype inheritance is dynamic and allows objects to change their prototype during runtime.
  • In classical inheritance, classes define objects, while JavaScript's prototype inheritance relies on objects and their prototypes.
  • JavaScript's prototype chain is single, while classical inheritance can involve multiple parent classes.
JavaScript's prototype inheritance is dynamic, which means you can modify an object's prototype at runtime, adding or removing properties and methods. Classical inheritance is typically static, where classes define the structure beforehand. This dynamic nature allows for greater flexibility but can also lead to unexpected behaviors if not managed properly.

Which method is used to handle the successful resolution of a Promise?

  • .then()
  • .catch()
  • .finally()
  • .resolve()
To handle the successful resolution of a Promise, you use the .then() method. This method allows you to specify what should happen once the Promise is fulfilled or successfully resolved. It takes a callback function as its argument, which gets executed when the Promise is resolved.

In a Node.js application, you need to perform several database operations consecutively, where each operation depends on the result of the previous one. How might you structure your asynchronous code to handle this scenario efficiently?

  • Use Promises and "Promise.all"
  • Utilize nested callbacks
  • Implement async generators
  • Employ event emitters
To efficiently handle consecutive database operations with dependencies in Node.js, you can structure your asynchronous code using Promises and "Promise.all." This approach allows you to create Promises for each operation and then use "Promise.all" to wait for all Promises to resolve. It ensures that operations execute in the correct order and that you can handle dependencies between them easily.

In which scenario might a closure be particularly useful?

  • When you want to declare a global variable.
  • When you want to protect variables from being modified.
  • When you need to maintain access to local variables after the parent function has finished.
  • When you want to create a private method in an object.
Closures in JavaScript are particularly useful when you want to create private variables or methods in an object. They allow you to maintain access to local variables even after the parent function has completed its execution. Closures help with data encapsulation and information hiding.

The block of code inside ______ will be executed if its condition is true and all preceding conditions are false.

  • else-if
  • switch
  • try-catch
  • default
In a JavaScript "switch" statement, the block of code inside "default" will be executed if none of the preceding conditions (cases) match. It serves as a fallback or default option when none of the cases match the given expression.

How can you handle errors in the fetch API when using async/await syntax?

  • Use try/catch around the fetch call
  • Use the .catch() method on the Promise returned by fetch
  • Use a custom error handling function passed as a parameter to fetch
  • Errors are automatically handled by the fetch API when using async/await
When using async/await with the fetch API, you can handle errors by chaining a .catch() method onto the Promise returned by fetch. This allows you to catch any network or request-related errors and handle them gracefully. Using try/catch around the fetch call is not effective in capturing asynchronous errors that occur within the Promise. The fetch API does not automatically handle errors when using async/await.

What potential issue might arise when using arrow functions in methods within a class definition?

  • The "this" context is unpredictable and may lead to bugs.
  • Arrow functions cannot be used within class methods.
  • Arrow functions always cause memory leaks.
  • Arrow functions make the code less readable.
When arrow functions are used in methods within a class, the "this" context is fixed to the outer scope, which may lead to unexpected behavior. This can be problematic when trying to access class properties or methods, potentially introducing bugs. Developers need to be cautious when choosing arrow functions in this context.

What is the default binding of "this" in JavaScript?

  • Global object
  • Parent object
  • Undefined
  • Null
The default binding of "this" in JavaScript is the global object. In most cases, when "this" is not explicitly set or bound to an object, it defaults to the global object (e.g., "window" in browsers). This behavior can lead to unexpected results, so it's crucial to be aware of it and handle "this" appropriately in your code.