In ES6, a module imported via a relative path starting with ______ indicates it is located in the same directory.
- ./
- ../
- //
- \
When using a relative path in ES6 for module imports, ./ is used to indicate that the module is in the same directory as the importing module.
How would you use a for...of loop to iterate over a string?
- for (let char in myString)
- for (let char of myString)
- for (let i = 0; i < myString.length; i++)
- for (let i = myString.length - 1; i >= 0; i--)
The correct syntax to iterate over a string using a for...of loop is for (let char of myString). This loop simplifies the process of iterating over the characters of a string without the need for an index.
Which method is used to catch errors in a Promise?
- catchError
- errorHandler
- catch
- handleError
The correct method to catch errors in a Promise is the catch method. It is used to handle both synchronous and asynchronous errors that may occur during the execution of the Promise. By chaining the catch method to a Promise, you can specify a callback function to handle the errors and take appropriate actions.
Can dynamic imports be conditional? If so, what is a real-world use case?
- Yes, based on browser features
- No, always loaded unconditionally
- Yes, based on time of day
- No, only for large modules
Dynamic imports can be conditional, for example, based on browser features. This allows loading different code based on the capabilities of the user's browser, optimizing performance and providing a better user experience.
What is the result of using a Symbol as a key in a JSON object when using JSON.stringify?
- It serializes the symbol key along with its associated value.
- It ignores the symbol key and only serializes other enumerable properties.
- It throws an error since symbols cannot be used as keys in JSON objects.
- It converts the symbol key to a string and serializes it.
When using JSON.stringify, symbols are ignored, and only other enumerable properties are serialized. Symbols are not convertible to strings in this context.
What kind of exports are more conducive to effective tree shaking?
- Default exports
- Named exports
- All exports are treated equally by tree shaking.
- Both default and named exports
Tree shaking is more effective with named exports, as it can selectively include or exclude specific exports based on usage. Default exports can still be tree shaken, but named exports provide more granularity.
A function that takes another function as an argument is called a __________ function.
- Callback
- Higher-order
- Nested
- Recursive
In JavaScript, a higher-order function is a function that takes one or more functions as arguments or returns a function. It enables the use of functions as data, promoting a more functional and modular approach.
What happens if you await a function that does not return a Promise?
- It throws a syntax error
- The program crashes
- It works as expected
- It returns a rejected Promise
If you await a function that does not return a Promise, it will still work as expected. JavaScript automatically wraps the non-Promise value in a resolved Promise, allowing seamless integration of async/await with traditional synchronous functions.
Using _________ functions helps in maintaining function purity by not altering the original data structure.
- Map
- Reduce
- Pure
- Impure
Using pure functions helps in maintaining function purity by not altering the original data structure. Pure functions always produce the same output for the same input and do not modify external state, contributing to the predictability and reliability of the code.
To convert a Set into an array, use the spread operator like this: [..._________].
- Set
- Map
- Iterable
- Collection
To convert a Set into an array, use the spread operator like this: [...Set]. Sets in ES6 are iterable, and using the spread operator allows you to convert the Set into an array easily.
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.
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.