What types of collections can the for...of loop iterate over in JavaScript?

  • Arrays
  • Objects
  • Strings
  • All of the above
The for...of loop can iterate over iterable objects like arrays, strings, maps, sets, etc. However, it does not work with non-iterable objects like plain objects (use for...in for objects).

In order for tree shaking to work effectively, the module bundler must support _______ analysis.

  • Static
  • Dynamic
  • Runtime
  • Bundle
The correct option is (a) Static. Effective tree shaking relies on static analysis of the code by the module bundler. It involves analyzing the code without executing it, allowing the bundler to determine which parts of the code are unreachable and can be safely eliminated during the bundling process.

Can dynamic imports be used in place of traditional static imports?

  • Yes, always
  • No, never
  • It depends on the use case
  • Only in Node.js environments
Dynamic imports cannot always replace traditional static imports. They are suitable for scenarios where modules are needed dynamically at runtime, but static imports are still necessary for modules required during the application's initialization.

When constructing an object to manage user data, how do enhanced object literals in ES6 simplify the process?

  • Enhances readability and reduces boilerplate code
  • Provides better encapsulation and supports private variables
  • Enables easier prototypal inheritance
  • Allows dynamic property assignment with concise syntax
In ES6, enhanced object literals simplify the process of constructing objects by enhancing readability and reducing boilerplate code. The concise syntax makes it easier to declare properties and methods, leading to more maintainable code. This feature helps in creating cleaner and more efficient object initialization, especially in scenarios involving user data management.

ES6 Modules are automatically in __________ mode, in contrast to CommonJS modules.

  • Strict
  • Non-strict
  • Loose
  • Synchronous
ES6 Modules are automatically in strict mode, which enforces a stricter set of rules compared to non-strict mode. In contrast, CommonJS modules are not automatically in strict mode, and developers need to opt into strict mode if desired.

In a single-page application, how can dynamic imports optimize loading times for different components?

  • Load components synchronously in the main bundle.
  • Use dynamic imports to load components lazily when they are needed.
  • Preload all components during the initial page load.
  • Use static imports to include all components in the main bundle.
Dynamic imports allow loading components asynchronously when they are needed, reducing the initial loading time and improving performance. This is particularly useful in single-page applications where not all components are required immediately.

What method would you use to get the number of elements in a Map?

  • size method
  • length method
  • count method
  • elements method
To get the number of elements in a Map in ES6, you would use the size method. This method returns the number of key-value pairs present in the Map.

Which method is essential for an object to be an iterator?

  • next() method
  • getIterator() method
  • iterate() method
  • forEach() method
An iterator in JavaScript must implement the next() method. This method is called on each iteration and should return an object with properties like value and done, indicating the next value in the sequence and whether the iteration is complete.

How do dynamic imports interact with tree shaking in modern JavaScript bundlers?

  • Enhances tree shaking
  • Impedes tree shaking
  • No interaction
  • Works only in development mode
Dynamic imports enhance tree shaking by enabling the removal of unused code during the bundling process. Tree shaking is more effective as it can analyze the dynamic imports and eliminate dead code paths, leading to smaller bundle sizes.

The String.raw tag function returns a string where escape sequences like n are treated as _______ text.

  • Formatted
  • Raw
  • Escaped
  • Plain
The String.raw tag function in ES6 returns a string where escape sequences are treated as raw text. It is useful when you want to include backslashes in a string without interpreting them as escape characters.