What is a key difference between functional composition and method chaining?
- Method chaining involves calling methods directly on an object, while functional composition combines functions to create new functions
- Functional composition is only applicable in functional programming languages
- Method chaining can only be used with asynchronous functions
- All functions used in functional composition must be pure functions
Functional composition involves combining functions to create more complex ones, whereas method chaining is about calling methods directly on an object. Understanding this difference is crucial for designing clean and readable code.
What keyword is used to define a generator function in JavaScript?
- function*
- gen
- generator
- yield
The correct keyword to define a generator function in JavaScript is function*. It uses the asterisk (*) after the function keyword. This syntax indicates that the function is a generator. Options b) and c) are incorrect, and d) is used within the generator function to yield values.
In a WeakMap, keys must be ________ and not primitive values.
- Objects
- Strings
- Numbers
- Booleans
In a WeakMap, keys must be objects, as primitive values are not allowed as keys. This is because WeakMap uses "weak" references to the keys, and primitive values do not have references like objects.
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.
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.