In a project that aims to run both on the browser and Node.js with minimal changes, how would the choice between ES6 Modules and CommonJS affect the architecture?

  • ES6 Modules
  • CommonJS
  • No significant impact on architecture
  • Depends on the specific project requirements
Choosing ES6 Modules facilitates a more seamless transition between browser and Node.js environments due to its standardized syntax and support for asynchronous module loading. CommonJS, with its synchronous nature, might require additional efforts to adapt to the asynchronous nature of browser environments.

What is a key difference in how forEach and map handle array elements?

  • forEach returns a new array, while map mutates the original array
  • forEach is synchronous, while map is asynchronous
  • forEach is purely for iteration, while map is for transformation and returns a new array
  • map modifies the original array in place, while forEach creates a new array
The key difference is that forEach is primarily for iteration and doesn't return a new array, whereas map is used for transformation and creates a new array with the results of applying a function to each element of the original array.

How does the use of mixins and composition in ES6 aid in code reusability and maintainability?

  • By enabling the combination of behaviors from multiple sources
  • By reducing redundancy and promoting a modular approach
  • By providing a way to reuse and extend classes
  • By facilitating the creation of new classes with desired functionalities
In ES6, mixins and composition allow developers to combine functionalities from different sources, promoting code reusability. Mixins help in reducing redundancy, while composition enables the creation of modular and maintainable code.

When a Promise is rejected, which method is typically used to handle the rejection?

  • catch
  • finally
  • reject
  • onReject
In the context of Promises, the catch method is commonly used to handle the rejection of a Promise. It allows you to specify a callback function that will be called if the Promise is rejected, providing a way to handle errors in asynchronous operations.

Consider a scenario where a module is exporting multiple functions, but only some are used. How does tree shaking impact this scenario?

  • Only the used functions will be included in the final bundle, reducing its size.
  • All exported functions will be included in the bundle.
  • Tree shaking is not applicable to modules with multiple functions.
  • The entire module will be excluded from the bundle.
Tree shaking analyzes the code to include only the necessary parts. In this scenario, only the functions that are used will be included, resulting in a smaller bundle size. The other options are incorrect, as tree shaking specifically targets unused code.

To handle multiple Promises concurrently and respond when the first one is fulfilled, use Promise._________.

  • all()
  • race()
  • any()
  • first()
The correct method is race(). It returns a promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects, with the value or reason from that promise. This is useful for handling scenarios where you want to respond to the first promise that resolves.

How does a Set object handle NaN values?

  • NaN is not allowed in a Set.
  • It treats all NaN values as equal.
  • Each NaN value is treated as a unique value in the Set.
  • It throws an error when trying to add NaN.
Sets treat NaN values as equal. Therefore, adding multiple NaN values to a Set will result in having only one NaN value in the Set.

How does the static method Promise.all() work with multiple promises?

  • Executes all promises in parallel and returns a single promise that resolves when all promises are fulfilled or rejects when any promise is rejected.
  • Executes promises sequentially and returns the result of the last promise.
  • Randomly selects one promise to execute and returns its result.
  • Executes promises concurrently but returns the result of the first fulfilled promise.
The Promise.all() method takes an array of promises, executes them in parallel, and returns a single promise that resolves when all the input promises have fulfilled or rejects when any one of them is rejected. This is useful for coordinating multiple asynchronous operations.

To import a default export, use import ______ from 'module-name';.

  • defaultImport
  • importDefault
  • importDefaultExport
  • importDefaultModule
In ES6, when importing a default export from a module, you use the syntax import name from 'module-name';. The keyword default is not used, making option b) importDefault the correct choice.

How can the spread operator be used to create a shallow copy of an object in ES6?

  • Object.assign({}, originalObject)
  • originalObject.clone()
  • originalObject.spread()
  • Object.copy(originalObject)
The spread operator (...) can be used to create a shallow copy of an object by spreading its properties into a new object literal. Object.assign({}, originalObject) is a common pattern for creating a shallow copy in ES6.