Q3: Consider an application that monitors the status of DOM elements. How would a WeakSet be beneficial in this context?
- WeakSet
- Set
- Both WeakSet and Set
- Neither WeakSet nor Set
In the context of monitoring DOM element status, a WeakSet is beneficial. This is because a WeakSet can hold references to DOM elements without preventing them from being garbage collected when they are removed from the DOM. As DOM elements are added and removed dynamically, a WeakSet ensures that the references do not cause memory leaks. If a DOM element is removed from the document, its reference in the WeakSet becomes automatically eligible for garbage collection. A Set, on the other hand, would keep references even if the DOM elements are removed, potentially leading to memory issues in the long run.
Arrow functions should not be used as ________ because they cannot be used with the new operator.
- Constructors
- Event Listeners
- Callbacks
- Promises
Arrow functions are not suitable for use as constructors since they lack a prototype property and cannot be used with the new operator. They are primarily designed for concise function expressions.
When using Promise.all(), if any promise is rejected, the entire operation is considered _________.
- resolved
- pending
- rejected
- fulfilled
If any promise in the iterable passed to Promise.all() is rejected, the entire operation is considered rejected. All other promises are ignored, and the Promise.all() itself is rejected with the reason of the first rejected promise.
How does the yield keyword function in the context of iterators and iterables?
- Pauses the execution and returns a value to the iterator
- Ends the iterator immediately
- Skips the next iteration
- Continues the execution without interruption
In the context of iterators and iterables, the yield keyword is used to pause the execution of a generator function, returning a value to the iterator. When the generator is later called again, it resumes execution from where it was paused.
When applied to a string, the spread operator will split it into individual '__________'.
- Characters
- Substrings
- Arrays
- Elements
When applied to a string, the spread operator (...) will split it into individual characters, creating an array of characters. This is useful for tasks where you need to manipulate individual characters in a string or convert a string into an array of characters.
What is a use case for choosing WeakMap over Map?
- When the keys need to be weakly held and automatically removed
- When the keys have a strong reference and need to be retained
- When the map needs to be iterated in a predictable order
- When the map requires frequent updates
A use case for choosing WeakMap over Map is when the keys need to be weakly held and automatically removed when they are no longer referenced elsewhere in the code. This is beneficial for scenarios where precise memory management is crucial.
What is the behavior of await in a loop when iterating over a set of asynchronous tasks?
- Pauses the loop until the asynchronous task is complete
- Skips the iteration with the unresolved promise
- Throws an error
- Continues the loop without waiting
When using await in a loop, it pauses the execution of the loop until the asynchronous task is complete. This ensures that the subsequent iterations don't start until the current one finishes. This behavior is crucial to avoid race conditions and maintain the desired order of execution in asynchronous operations.
A rejected promise will skip all subsequent .then() methods until a _________ method is found.
- finally()
- catch()
- reject()
- resolve()
The correct method is catch(). When a Promise is rejected, it will skip all subsequent then() methods and look for a catch() method in the chain to handle the rejection. The catch() method is used to specify what to do in case of a rejected Promise.
Using yield* within a generator function delegates to another _________ or iterable object.
- Generator
- Function
- Object
- Array
In JavaScript, the yield* statement is used within a generator function to delegate to another generator or iterable object. The yield* expression iterates over the provided iterable, effectively delegating to each element's value.
Consider a situation where you have an array of user objects. How would you implement an iterator to selectively process only certain users?
- Use the filter method to create a new array with the selected users
- Implement a custom iterator with a conditional statement inside the next method
- Use the map method to selectively process users
- Utilize the reduce method to iterate and selectively process users
Implementing a custom iterator is a powerful approach in this scenario. By creating a custom iterator, you have fine-grained control over the iteration process, allowing you to selectively process specific users based on the conditions defined in the iterator's next method.
If you have a configuration object that should not be altered during the execution of a program, how would you declare it using ES6 syntax?
- Declare using let
- Declare using const
- Declare using var
- Declare using readonly (ES6 feature)
You would declare the configuration object using const, as it ensures the object remains constant throughout the program execution, preventing unintended changes.
What method is used to specify the code to execute after a Promise is fulfilled?
- then()
- catch()
- resolve()
- reject()
The then() method is used to specify the code that should be executed after a Promise is fulfilled. It allows you to handle the successful resolution of a Promise and work with the result.