How does garbage collection in JavaScript affect WeakSet?
- WeakSet does not participate in garbage collection
- WeakSet triggers garbage collection on its own
- Garbage collection has no impact on WeakSet
- WeakSet entries are explicitly removed during garbage collection
Garbage collection has no impact on WeakSet. Unlike traditional sets, WeakSet does not prevent the referenced objects from being garbage-collected, making it useful in scenarios where you don't want to retain objects solely because they are part of a set.
In Promises, the _______ function is used to transition a promise into a resolved state, while _______ is used for rejection.
- resolve
- then
- reject
- catch
The correct option is 'resolve.' The resolve function transitions a promise into a resolved state, fulfilling the promise. On the other hand, the 'catch' function is used to handle rejection and is associated with the 'reject' state. The 'then' function is used for chaining and handles both fulfillment and rejection.
What is the default behavior of this at the top level in ES6 Modules, as opposed to CommonJS modules?
- Lexical scoping
- Dynamic scoping
- No scoping
- Global scoping
In ES6 Modules, the this keyword behaves according to lexical scoping, meaning it retains the value of this from the surrounding scope. This is different from CommonJS modules, where this refers to the exports object.
In what way does the Spread Operator interact with iterables?
- Spreads the iterable elements into a new array
- Converts the iterable into a string
- Ignores the iterable elements
- Creates a deep copy of the iterable
The Spread Operator (...) in ES6 is used to spread the elements of an iterable (e.g., an array) into a new array. It provides a concise way to concatenate arrays, create shallow copies, and pass array elements as function arguments.
In a for...of loop, using let allows each iteration to have its own _________ scope.
- Global
- Block
- Local
- Function
The let keyword creates a block-scoped variable, so each iteration in a for...of loop has its own block scope. This helps prevent unintended variable hoisting and makes the code more predictable.
Can you use destructuring assignment to assign default values to variables?
- Yes
- No
- Only for primitive data types
- Only for arrays
Yes, destructuring assignment in ES6 allows you to assign default values to variables. If the assigned value is undefined, the variable takes the default value specified. This feature enhances flexibility in handling variable assignments.
What is the main difference between default exports and named exports in ES6 modules?
- Default exports are used for exporting a single value or function, while named exports are used for exporting multiple values or functions from a module.
- Default exports use the export default syntax, while named exports use the export { myFunction }; syntax.
- Default exports are mandatory, while named exports are optional.
- Default exports can be renamed during import using the as keyword, while named exports cannot be renamed.
The main difference is that default exports are for a single value or function, and named exports are for multiple values or functions. Default exports use export default while named exports use export { myFunction };.
What happens when you try to import a non-existent named export from a module?
- It will result in an error at runtime.
- It will import the default export instead.
- It will create an empty object for the named export.
- It will import all exports from the module.
If you try to import a non-existent named export, it will result in an error at runtime. It's important to ensure that the named exports you are trying to import actually exist in the module.
How does ES6 handle importing a module that is located in a node_modules folder?
- Looks for the module in the current directory first, then checks the node_modules folder
- Directly looks in the node_modules folder
- Prioritizes searching in the node_modules folder before checking the current directory
- Searches both the current directory and the node_modules folder simultaneously
In ES6, when importing a module, the resolution algorithm prioritizes searching the node_modules folder before checking the current directory. This allows for better organization of dependencies and avoids naming conflicts with local modules. It simplifies the module import process and enhances code maintainability by following a standardized approach to handle third-party libraries.
ES6 arrow functions can make recursive functions more concise and _________.
- Readable
- Performant
- Maintainable
- Shorter
ES6 arrow functions, with their concise syntax, can make recursive functions shorter and more readable by eliminating the need for the 'function' keyword and providing implicit returns.