When a generator function is called, what is returned?
- Generator object
- Array
- Promise
- String
When a generator function is called, it returns a Generator object. This object can be used to control the execution of the generator using methods like next(). Options b), c), and d) are incorrect as they do not represent the result of calling a generator function.
One primary difference between WeakMap and Map is that WeakMap keys are ________ to garbage collection.
- Referenced
- Immutable
- Strongly bound
- Weakly bound
In a WeakMap, the keys are weakly held, meaning that they don't prevent the referenced objects from being garbage collected. This is particularly useful when using objects as keys, as it allows them to be garbage collected when they are no longer needed.
For side effects only, without importing any variables, use import _________ from 'module-name'.
- { sideEffect }
- sideEffect
- * as sideEffect
- sideEffect as default
In this scenario, the correct option is c) * as sideEffect. This syntax allows importing all exports from the 'module-name' without explicitly naming them.
In ES6, what is the behavior of using super in a method when extending a class?
- Calls the superclass constructor
- References the superclass prototype
- Creates a new instance of the superclass
- Invokes the superclass method
In ES6, when using super in a method of a subclass, it refers to the superclass, allowing access to its methods and properties. It is often used to call the superclass constructor using super() or invoke methods on the superclass. This facilitates extending the functionality of the superclass.
When creating a utility class with common helper functions, how would you ensure that these methods are accessible without instantiating the class?
- Use static methods
- Use private methods
- Use public methods
- Use protected methods
In ES6, you can ensure that methods in a utility class are accessible without instantiating the class by defining them as static methods. Static methods are associated with the class itself rather than instances, allowing you to call them directly on the class without creating an object. This is beneficial for utility classes where instantiation is unnecessary, and you want to organize related functions.
Can you nest template literals within each other?
- Yes
- No
- Only if they have the same variables
- Only in arrow functions
Template literals can be nested within each other, allowing for more complex string constructions. This nesting can be useful when building strings that involve multiple layers of dynamic content.
Describe the flow of execution in a JavaScript program that includes both synchronous logging and asynchronous API calls.
- The synchronous code is executed first, followed by the asynchronous API call. The API call is offloaded to the browser's APIs, and its callback is queued in the task queue. Once the call stack is empty, the event loop picks up the callback, and its execution is prioritized.
- Synchronous logging occurs immediately in the call stack, while the asynchronous API call is offloaded to the browser's APIs. The event loop ensures the callback is queued and executed once the call stack is empty, allowing non-blocking behavior.
- The JavaScript program executes synchronous logging first, and then the asynchronous API call is delegated to the browser's APIs. The event loop monitors the call stack and task queue, ensuring the asynchronous callback is prioritized for execution.
- The event loop manages the asynchronous API call, queuing its callback, and ensuring it is executed once the call stack is empty. Synchronous logging happens directly in the call stack, maintaining a smooth flow of execution in the JavaScript program.
The flow of execution in a JavaScript program involves synchronous logging and asynchronous API calls. The synchronous code is executed first, followed by the asynchronous API call, which is offloaded to the browser's APIs. The event loop ensures that the callback is queued and prioritized for execution once the call stack is empty. Understanding this flow is essential for handling mixed synchronous and asynchronous operations effectively.
Using _________, you can condense an array into a single value, optionally starting with an initial value.
- map
- reduce
- filter
- forEach
The correct answer is reduce. This method allows you to condense the elements of an array into a single value, using a provided function. It is particularly useful for tasks like summing values or finding the maximum element.
Asynchronous callbacks are placed in a queue known as the __________ queue.
- Callback Queue
- Event Queue
- Promise Queue
- Execution Queue
Asynchronous callbacks are placed in the Callback Queue, and they wait for the Event Loop to move them to the Call Stack for execution when the stack is empty.
Is it possible to destructure properties from nested objects?
- Yes
- No
- Only if the nested object has a single property
- Only if the nested object is declared as a constant
Yes, in ES6, destructuring assignment can be used to extract properties from nested objects. This allows for a more convenient and concise way of accessing values within nested structures, enhancing code simplicity.