How does a pure function handle dependencies or external variables?
- Manages dependencies internally
- Avoids external dependencies
- Handles dependencies through callbacks
- Utilizes global variables
In a pure function, dependencies are avoided to maintain purity. External variables could introduce side effects, violating the principle of purity.
To define a function as a method in an object literal, ES6 syntax does not require the ________ keyword.
- function
- method
- this
- function
In ES6, when defining a method in an object literal, you can use the concise method syntax by omitting the "function" keyword, making the code more succinct and aligned with modern practices.
How does caching of modules work differently in ES6 Modules compared to CommonJS?
- ES6 Modules use a global cache for all modules
- ES6 Modules have no caching mechanism
- Each ES6 Module has its own cache
- CommonJS has no module caching
In ES6 Modules, each module maintains its own cache, preventing duplication of work and ensuring consistency across the application. CommonJS, on the other hand, uses a global cache for all modules.
Which of the following best describes the purpose of functional composition?
- Combining small, simple functions to build more complex ones
- Declaring variables globally for easy access
- Utilizing conditional statements for decision-making
- Executing functions in parallel for efficiency
The purpose of functional composition is to combine small, simple functions to build more complex and reusable functions. By composing functions together, developers can create a flow of data transformation, leading to cleaner and more maintainable code. This approach aligns with the principles of functional programming, emphasizing immutability and pure functions. A strong grasp of functional composition is essential for developers aiming to write expressive and efficient JavaScript code.
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.
How does tree shaking differ between named and default exports?
- Tree shaking is more effective with default exports
- Tree shaking is more effective with named exports
- Tree shaking treats both equally
- Tree shaking cannot be used with ES6 modules
Tree shaking is more effective with default exports because the bundler can directly eliminate the unused default export, whereas with named exports, the bundler may include unnecessary exports even if only one is used.
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 };.