Pure functions should not modify any _________ state.
- Local
- External
- Internal
- Global
Pure functions should not have side effects, and modifying global state is considered a side effect. Therefore, they should not modify any global state.
When using arrow functions with higher-order functions like Array.prototype.map, this will refer to the ________ where the arrow function was defined.
- Global Object
- Callback Function
- Scope Chain
- Enclosing Function
Arrow functions capture the this value from their surrounding lexical scope. In the context of higher-order functions, this is crucial to understanding and avoiding potential issues.
In what scenario would a WeakSet be more appropriate than a Set for managing DOM elements?
- When managing temporary or transient DOM elements that should not prevent garbage collection.
- WeakSet is preferred for static and permanent DOM elements to enhance performance.
- In scenarios where strict ordering of DOM elements is crucial.
- When managing a small number of DOM elements with known lifetimes.
WeakSet is suitable for scenarios where the collection of DOM elements may vary, and automatic garbage collection is desired.
How can you import an entire module as an object in ES6?
- import * as myModule from './myModule';
- import { myModule } from './myModule';
- import myModule from './myModule';
- import { * } from './myModule';
In ES6, you can use the import * as alias syntax to import the entire module as an object. This allows you to access all exported values through the alias object.
How does static analysis contribute to tree shaking in ES6 modules?
- It dynamically analyzes the code at runtime to identify unused modules
- It performs analysis at compile-time to identify and eliminate unused exports
- It relies on dynamic imports to load only necessary modules
- It statically enforces tree shaking by default in ES6
Static analysis in tree shaking occurs during the compilation phase. It identifies and eliminates unused exports by analyzing the code without executing it. This helps in reducing the bundle size by excluding unnecessary code paths and dependencies.
The _______ operator can be used to unpack the values from an iterable into individual elements.
- spread
- merge
- combine
- join
The spread operator (...) in JavaScript is used to unpack elements from an iterable, such as an array, and spread them into individual elements or combine them with other elements.
What happens when you try to create a new Symbol using the new keyword?
- Error is thrown
- A new Symbol is created
- It returns an existing Symbol
- Symbols cannot be created using new
When attempting to create a Symbol using the new keyword, it results in an error. Symbols are primitive values, and using the new keyword with them is not allowed. Symbols are typically created without the new keyword.
In a project using ES6 modules, if you need to dynamically load a module based on a user's action, which import method would you use?
- import()
- require()
- import dynamic
- import lazy
In ES6, the import() function allows dynamic loading of modules. It returns a promise that resolves to the module namespace object, allowing you to load modules conditionally or based on user actions.
If you are implementing a function that will be used as a callback, which might benefit from lexical scoping of this, what type of function would you choose?
- Arrow function
- Regular function
- Class method
- Anonymous function
Arrow functions are suitable for callbacks, as they capture the "this" value from their enclosing scope, providing a clean and concise way to maintain lexical scoping.
When destructuring an array, the syntax _________ is used to skip over elements.
- ...
- ++
- //
- --
Destructuring an array in ES6 allows for skipping elements using the spread/rest syntax .... This is particularly useful when certain elements are not needed in the assignment.