An unhandled promise rejection can be caught globally using process.on('________', handler).
- rejection
- unhandledRejection
- promiseError
- errorHandled
In Node.js, the 'unhandledRejection' event is emitted when a Promise is rejected but no handler is provided. Using process.on('unhandledRejection', handler) allows you to catch and handle such rejections globally.
What is the primary purpose of tree shaking in JavaScript modules?
- Eliminating Unused Code
- Minifying the Code
- Enhancing Code Readability
- Improving Network Performance
Tree shaking is a process in JavaScript where unused or dead code is eliminated from the final bundle during the build process. The primary purpose is to reduce the size of the bundle by removing parts of the code that are not actually used, thus improving the application's performance and loading times. It helps in optimizing the overall efficiency of the application by excluding unnecessary code.
Higher-order functions improve code __________ by allowing for more abstract and concise code.
- Efficiency
- Readability
- Performance
- Execution
By using higher-order functions, code becomes more readable and expressive. It abstracts away the details of how a particular operation is performed, making the code concise and easier to understand.
How does inheritance of static methods differ from that of instance methods in ES6?
- Static methods cannot be inherited.
- Static methods are inherited through the prototype chain.
- Static methods are inherited using the extends keyword.
- Static methods are inherited through the super keyword.
In ES6, static methods are inherited through the prototype chain, just like instance methods. The key difference lies in how they are called and accessed. The extends keyword is used to inherit static methods, making them accessible through the derived class. Option B is the correct answer.
What is the primary purpose of the async keyword in a function declaration?
- Enables the function to return a Promise
- Specifies that the function can be called asynchronously
- Defines a function that can only be executed in an asynchronous manner
- Marks a function as an asynchronous event handler
The async keyword is used to make a function return a Promise. This allows the use of await within the function, indicating that it contains asynchronous code.
How does dynamic import (import()) differ from static import (import ... from) in ES6?
- Deferred loading at runtime
- Resolved during compilation
- Allows importing entire modules as a single object
- Used for importing only specific exports
Dynamic import allows loading a module at runtime, which can be beneficial for code splitting and lazy loading. Static import, on the other hand, is resolved during compilation.
In a project management tool, you need to ensure each task is unique but also maintain their insertion order. Which data structure would you use?
- Set
- Array
- WeakSet
- Map
In this case, an Array is the most appropriate choice. It allows you to maintain the order of tasks while ensuring uniqueness. Unlike Set, which only stores unique values without any specific order, an Array meets both requirements for this project management tool.
If you are required to iterate over a collection of objects representing users and perform an action on each, how would a for...of loop benefit over other loops?
- for loop
- for...in loop
- forEach loop
- for...of loop
The correct option is the for...of loop. It is specifically designed for iterating over iterable objects and works well with arrays and other collections. It directly provides the values, avoiding the need for indexing and making the code more readable. This is especially beneficial when iterating over objects representing users, as it simplifies the syntax and enhances code clarity.
Consider a scenario where you have to implement a function to compute factorial numbers. How would ES6 features enhance the implementation of this recursive function?
- Utilize the let and const declarations for improved variable scoping and clarity.
- Apply the var declaration for simplicity and compatibility.
- Use the arrow function syntax for concise and expressive code.
- Implement the function using the function keyword for better readability.
ES6 features like let and const provide block-level scoping, enhancing the clarity of variables in the recursive factorial function. The arrow function syntax also contributes to code conciseness, making the implementation more elegant. The other options either introduce potential issues or miss the advantages of ES6 features.
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.