When using named exports, the import names must _________ the exported names unless using the as keyword.

  • exactly match
  • differ from
  • be optional
  • be prefixed with 'import'
When using named exports, the import names must exactly match the exported names unless using the 'as' keyword.

How is a class method defined in ES6?

  • function methodName() { }
  • method methodName() { }
  • methodName() { }
  • class methodName() { }
In ES6, a class method is defined without the function keyword. It is declared directly within the class body using the syntax methodName() { }. This syntax is concise and aligns with modern JavaScript practices.

How does immutability relate to pure functions in JavaScript?

  • Enhances predictability
  • Minimizes side effects
  • Allows for asynchronous operations
  • Supports object mutation
In JavaScript, immutability is closely tied to the concept of pure functions. Pure functions do not modify external state, which promotes predictability and makes it easier to reason about code. Immutability ensures that once a data structure is created, it cannot be changed, aligning with the principles of pure functions.

Consider a scenario where you need to sequentially process a list of URLs to fetch data. How would you structure your async/await function to achieve this?

  • Use Promise.all for parallel processing
  • Utilize a loop with await inside to process URLs sequentially
  • Employ async.each for sequential URL processing
  • Mix Promise.all and Promise.race for optimized sequential processing
To sequentially process a list of URLs, structure your async/await function with a loop that utilizes await inside. This ensures each URL is processed one after the other, maintaining order and avoiding parallel execution.

What is the primary purpose of a constructor in a JavaScript class?

  • Initializing class properties
  • Defining class methods
  • Controlling class inheritance
  • Managing private class members
The primary purpose of a constructor is to initialize class properties, setting up the initial state of the object. Constructors are called when an object is created, ensuring that the object starts with the desired values.

In a Promise chain, where should you place a .catch() method for centralized error handling?

  • At the beginning of the Promise chain.
  • At the end of the Promise chain.
  • It can be placed anywhere within the Promise chain.
  • Immediately after the last then() block in the Promise chain.
To achieve centralized error handling in a Promise chain, the .catch() method should be placed immediately after the last then() block. This ensures that any error thrown at any stage of the Promise chain is caught centrally, providing a clean and organized way to handle errors in asynchronous operations.

Can you enumerate the keys of a WeakMap?

  • Yes, you can use the getKeys method.
  • No, WeakMaps do not have a method to retrieve all keys.
  • Yes, you can use a loop to iterate over the keys.
  • No, keys in WeakMaps cannot be enumerated.
WeakMaps prioritize privacy by not providing a direct way to enumerate keys, contributing to their use in scenarios where key visibility is a concern.

If you have a utility class for a shopping application, how would you implement a method that calculates the total discount for all users?

  • Implement a static method within the utility class to calculate discounts.
  • Use an instance method and iterate through each user, calculating and accumulating discounts.
  • Create a separate discount calculation class and delegate the task to it.
  • Utilize a global variable to store the total discount and update it in each user instance.
In a scenario like a shopping application, it's beneficial to use a static method within the utility class to calculate the total discount. This way, the method is associated with the class itself, not an instance, and can be easily accessed without creating unnecessary instances for the calculation. Static methods are appropriate for operations that don't depend on the state of a specific instance.

To import only a part of a module, use the __________ syntax in ES6.

  • import {specificPart} from
  • include {specificPart} from
  • load {specificPart} from
  • require {specificPart} from
In ES6, when importing only a part of a module, you use the {} syntax. For example, import {specificPart} from 'moduleName'; allows you to import only the specified part of the module.

What is the role of the .catch() method in Promise chaining?

  • It is used to handle errors that occur in the promise chain.
  • It is only applicable to synchronous code.
  • It can only be used once in a promise chain.
  • It must always be placed before any .then() block.
The .catch() method in Promise chaining is used to handle errors that occur anywhere in the promise chain. It ensures that errors are caught and appropriate actions are taken to handle them gracefully.