Question 3: When setting up a mono-repo with multiple packages, how does ES6 module resolution impact the sharing of code across different packages?
- Facilitates code sharing through relative imports
- Hinders code sharing across packages
- Enables code sharing through global imports
- Requires a centralized package for sharing
In a mono-repo with multiple packages, using relative imports in ES6 modules can facilitate code sharing. It allows packages to import modules from one another directly, enhancing modularity and reusability. This approach avoids a centralized package and promotes a more decentralized and modular code organization.
How does the ES6 specification handle tail call optimization and what are its limitations in JavaScript implementations?
- ES6 specification doesn't explicitly mandate tail call optimization.
- It mandates tail call optimization, but its implementation varies.
- Tail call optimization is not supported in ES6.
- It is only applicable to specific cases in ES6.
In ES6, tail call optimization is mandated, but its implementation may vary across JavaScript engines. The limitation lies in inconsistent support across different environments, making it crucial to check engine compatibility when relying on this optimization.
Can destructuring assignment be used with arrays, and if so, how does it differ from object destructuring?
- Yes, and it works the same as with objects
- No, destructuring can only be used with objects
- Yes, but the syntax is different from object destructuring
- Yes, and it uses square brackets for the pattern
Destructuring assignment can be used with arrays in JavaScript. However, the syntax differs from object destructuring. With arrays, square brackets are used to indicate the pattern, making it distinct from object destructuring that uses curly braces.
What is the difference in execution timing between callbacks and Promises?
- Callbacks may lead to callback hell due to nested structures, affecting the execution sequence.
- Promises execute asynchronously, allowing better control over the flow of the program.
- Execution timing is the same for both callbacks and Promises.
- Callbacks always execute before Promises.
Promises provide a more straightforward approach to asynchronous programming, allowing developers to handle execution timing more efficiently. Callbacks, especially when nested, can lead to callback hell, making it challenging to manage the sequence of operations. This understanding is crucial for developers aiming to enhance code readability and manage asynchronous tasks effectively using Promises.
In functional programming, _________ is a technique used to avoid side effects by ensuring data is not modified.
- Mutable State
- Immutability
- Asynchronous
- Prototyping
In functional programming, immutability is a technique that involves not modifying the data once it is created. This helps avoid side effects and makes functions more predictable and easier to reason about. Immutability ensures that data remains constant throughout its lifecycle.
What is the syntax to export a single function from an ES6 module?
- export function myFunction() {}
- export myFunction from 'module';
- export { myFunction };
- export default myFunction;
In ES6, to export a single function, you use the syntax export { myFunction };. This allows you to import it individually in another module. The export default is used for the default export, not a named one.
When using relative paths in ES6 module imports, what does './' and '../' signify?
- Current directory
- Parent directory
- Root directory
- Child directory
In ES6 module imports, './' represents the current directory, and '../' represents the parent directory. Understanding these path notations is crucial for correctly referencing modules within the project structure.
Can a module have both named and default exports?
- Yes, a module can have both named and default exports simultaneously.
- No, a module must choose either named or default exports.
- Modules can have multiple default exports, but only one named export.
- Named exports can only be used if there are no default exports in the module.
In ES6 modules, it's possible for a module to have both named and default exports. This flexibility allows developers to export a single main entity using default export and also export additional values using named exports.
The "sideEffects" property in package.json helps tree shaking by indicating if a module may contain _______.
- Side Effects
- Pure Functions
- Impure Functions
- Async Functions
The correct option is (b) Pure Functions. The "sideEffects" property, when set to false, informs the module bundler that the module does not have side effects, making it eligible for tree shaking. Pure functions are side effect-free functions that only depend on their input parameters, making them suitable for elimination during tree shaking.
What happens if you try to redeclare a variable declared with let in the same scope?
- It generates an error
- It assigns a new value to the variable
- It creates a new variable with the same name
- It deletes the variable
When you attempt to redeclare a variable with let in the same scope, it will generate an error. This is because variables declared with let cannot be redeclared in the same scope.