The _________ method in Promise chaining is used to catch any errors that occur during the execution of the promise.
- reject()
- catch()
- handle()
- error()
The correct method is catch(). It is used in promise chaining to handle any errors that may occur during the execution of the promise. It is a best practice to include a catch() block to handle errors in promise chains.
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.
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.
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.