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.
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.
The ES6 Module system supports __________ import syntax, which is not possible in CommonJS.
- Dynamic
- Static
- Deferred
- Asynchronous
ES6 introduced static import syntax for modules, allowing developers to specify exactly what they want to import. This is not possible in CommonJS, where imports are dynamic and are resolved at runtime.
Imagine you're developing a real-time voting system, where each user can only vote once but you need to efficiently check if a user has already voted. What ES6 data structure would you implement for storing user IDs?
- Map
- Set
- WeakMap
- WeakSet
For a real-time voting system, a Set would be the best choice. A Set only allows unique values, making it efficient for storing user IDs and quickly checking if a user has already voted, ensuring each user can only vote once in the system.