If an arrow function is written with a single expression, the return value is the result of the expression without using the ________ keyword.
- Break
- Return
- Yield
- Exit
In arrow functions, if the function body consists of a single expression, you can omit the curly braces {} and the return keyword. The return value will be the result of the expression automatically.
Tree shaking is most effective when modules use _______ exports rather than wildcard exports.
- Named
- Default
- All
- Individual
Tree shaking is a technique in JavaScript used to eliminate unused code during the build process. When modules use named exports, it allows the bundler to selectively include only the functions or variables that are actually used, making tree shaking more effective. Default exports and wildcard exports can complicate this process, leading to less efficient tree shaking.
The method _________ is used to execute code after a Promise is either fulfilled or rejected.
- finally()
- done()
- complete()
- finalize()
The correct method is finally(). The finally() method is used to specify a function that will be executed regardless of whether the Promise is fulfilled or rejected. It allows you to perform cleanup or finalization tasks.
When importing a module without specifying a subpath, ES6 will by default look for a file named ________.
- main.js
- module.js
- index.js
- entry.js
If a specific subpath is not provided during the import, ES6 will default to looking for a file named index.js within the target module directory.
In the context of tree shaking, what is the significance of the "sideEffects" flag in a package.json file?
- Specifies files that should be excluded from the tree-shaking process
- Indicates whether the package has side effects that prevent tree shaking
- Determines the priority of modules during tree shaking
- Flags external dependencies for tree shaking
The "sideEffects" flag in package.json is used to inform the bundler about the side effects of a module. If set to false, it allows the bundler to perform aggressive tree shaking by eliminating unused exports. If set to true, it implies the module has side effects, and the bundler avoids tree shaking to maintain those side effects.
In a node.js application, how would you handle errors when performing multiple asynchronous operations in parallel?
- Implementing a try-catch block around each asynchronous operation
- Utilizing the Promise.all() method with a single catch block for error handling
- Using multiple catch blocks for each asynchronous operation
- Handling errors outside the asynchronous operations entirely
In the scenario of multiple asynchronous operations, using Promise.all() simplifies error handling by allowing a single catch block to capture errors from any of the parallel operations. This promotes cleaner and more concise error management, making it easier to identify and address issues in the asynchronous flow. Multiple catch blocks might lead to redundancy and make the code harder to maintain.
What is a key difference between a Map and a WeakMap in JavaScript?
- Both can store key-value pairs, but the keys in WeakMap must be objects, and they don't prevent the objects from being garbage collected.
- Maps allow any data type as keys, while WeakMaps only allow objects as keys.
- Maps are iterable, while WeakMaps are not iterable.
- WeakMaps have a getKeys method to retrieve all keys.
Maps are versatile and allow various data types as keys, while WeakMaps are designed for enhanced privacy, with keys limited to objects and no direct method for key retrieval.
If a module exports several named items, how can you import all of them at once?
- import * as allExports from 'module';
- import { export1, export2 } from 'module';
- import allExports from 'module';
- import { * } from 'module';
To import all named exports from a module at once, you use the syntax import * as allExports from 'module';. This creates an object containing all the exports, and you can access them using dot notation, e.g., allExports.exportName.
In terms of scope, how do variables and functions behave differently in ES6 Modules compared to CommonJS modules?
- Variables and functions have block scope
- Variables have block scope, functions have global scope
- Variables and functions have global scope
- Variables have global scope, functions have block scope
ES6 Modules introduce block scope for both variables and functions, offering better encapsulation. In contrast, CommonJS has function-level scope for variables and global scope for functions, leading to potential issues with variable leakage.
How does the Fetch API handle HTTP error statuses (like 404 or 500) in Promises?
- It doesn't reject the Promise for any HTTP error status
- It rejects the Promise only for network errors
- It rejects the Promise for any non-2xx HTTP status
- It triggers a catch block for network errors and HTTP errors
The Fetch API rejects the Promise for any non-2xx HTTP status, allowing developers to handle errors more effectively. Network errors, however, are still caught separately, providing detailed error handling in both scenarios.
In a custom iterable, the next() method should return an object with two properties: value and ________.
- done
- index
- result
- previous
In a custom iterable, the next() method should return an object with two properties: value, representing the current value in the iteration, and done, a boolean indicating whether the iteration is complete.
Consider a library that needs to add unique identifiers to objects without risk of property clashes. How would Symbols be utilized in this case?
- Using string identifiers
- Using Symbols as unique identifiers
- Using numbers as identifiers
- Using object literals with unique keys
The correct option is using Symbols as unique identifiers. Symbols provide a way to create unique keys for object properties, ensuring that the identifiers added by the library are distinct and not prone to clashes. Using string identifiers or numbers may lead to conflicts, especially in a shared library context. Object literals with unique keys can be used, but Symbols offer a more direct and elegant solution.