How does a for...of loop differ from a for...in loop in terms of iteration?
- for...of is used for arrays and iterable objects
- for...in is used for arrays and iterable objects
- for...of iterates over property values
- for...in iterates over property names
The for...of loop is designed specifically for iterating over values of iterable objects, such as arrays, while the for...in loop iterates over property names and is not limited to iterable objects.
In a scenario where an application needs to make several API calls and only proceed after all have successfully completed, what Promise method would be most appropriate?
- Promise.race
- Promise.all
- Promise.resolve
- Promise.reject
When dealing with multiple asynchronous operations, Promise.all is used to wait for all promises to be resolved. It ensures that the application proceeds only when all the promises are successfully completed, making it suitable for scenarios where multiple API calls need to be made concurrently.
What is the correct syntax to create an instance of a class in ES6?
- let obj = new Class();
- let obj = create(Class);
- let obj = Object.create(Class);
- let obj = Class.create();
The correct syntax to create an instance of a class in ES6 is to use the new keyword followed by the class name and parentheses, like this: let obj = new Class();. The new keyword is essential for creating instances and invoking the class constructor.
In ES6, how is a method defined inside a class?
- Using the function keyword
- Using the method keyword
- Using the def keyword
- Using the => arrow syntax
In ES6, methods inside a class are defined using the => arrow syntax. This syntax provides a concise and cleaner way to declare methods within class definitions. It binds the method to the instance, allowing easy access to the class properties.
To handle errors within a generator function, the _________ method can be used alongside next().
- throw
- catch
- try
- finally
To handle errors in a generator function, the throw method can be used alongside the next() method. When an error is thrown, it is caught by the nearest catch block, allowing for graceful error handling within the generator.
How does chaining array methods like map and filter affect performance?
- Negatively impacts performance due to multiple iterations
- Positively impacts performance by optimizing the code
- No significant impact on performance
- Only affects readability, not performance
Chaining array methods like map and filter can negatively impact performance due to multiple iterations. Each method creates a new array, and chaining them leads to intermediate arrays, causing additional iterations. This can be inefficient, especially for large datasets.
Imagine processing a shopping cart array to calculate the total price. Which method would efficiently achieve this?
- map
- reduce
- filter
- forEach
The most efficient method for calculating the total price in a shopping cart array is reduce. The reduce method iterates over each element of the array and accumulates a result. In this case, it can sum up the prices to provide the total cost.
What is a practical use case of currying in JavaScript?
- Simplifying function composition
- Handling asynchronous operations
- Memoization
- Event handling in UI
Currying simplifies function composition by breaking down functions into smaller, reusable units. This is useful for creating more modular and maintainable code, especially in functional programming paradigms.
How can dynamic imports be used to implement feature flags in a large-scale web application?
- Load all features during the initial page load.
- Use dynamic imports to conditionally load features based on feature flags.
- Hardcode feature flags directly in the application code.
- Use static imports to include all features in the main bundle.
Dynamic imports enable the implementation of feature flags by allowing the loading of features based on runtime conditions. This flexibility helps in enabling or disabling specific features without changing the core codebase.
Can a default export be imported with any name?
- Yes
- No
- Only in certain cases
- Depends on the file structure
In ES6, a default export can be imported with any name. The imported name is not dictated by the exporting module. It allows flexibility in naming while importing, making it more convenient.