What syntax is used to handle the promise returned by a dynamic import?
- await import('module')
- import('module').then()
- dynamicImport('module')
- import('module').catch()
The correct syntax to handle the promise returned by a dynamic import is import('module').then(). You use the then method to perform actions once the module has been successfully loaded. The catch method can be used for error handling if the dynamic import fails.
In functional programming, what is the recommended way to handle functions with side effects?
- Embrace side effects
- Minimize side effects
- Avoid functions altogether
- Encourage side effect propagation
In functional programming, it is recommended to minimize side effects. While it may be challenging to eliminate them entirely, minimizing side effects helps maintain the purity of functions and promotes a more declarative and predictable coding style. This approach contributes to better code maintainability and understandability.
What is a common pitfall when chaining multiple asynchronous operations using Promises?
- Not handling errors within each promise's chain.
- Mixing Promises with Callbacks.
- Using async/await inside a promise chain.
- Chaining promises without returning a new promise.
One common pitfall is forgetting to return a new promise when chaining promises. If a promise is not returned from each .then() callback, subsequent promises won't wait for the asynchronous operation to complete, potentially leading to unexpected behavior.
To pause and resume a generator function, the _________ keyword is used within the function body.
- yield
- await
- pause
- resume
In a generator function, the yield keyword is used to pause and resume the function's execution. It allows the generator to produce a sequence of values over time. The other options are not relevant to the purpose of pausing and resuming a generator.
Which of the following is a built-in higher-order function in JavaScript?
- map()
- add()
- concat()
- subtract()
The map() function in JavaScript is a built-in higher-order function that is commonly used to transform elements of an array. It takes a function as an argument and applies it to each element of the array.
In a scenario where a function calculates the total price of items in a cart, how would making this function pure affect its implementation?
- It would introduce side effects
- It would improve testability and predictability
- It would slow down the function execution
- It would require additional dependencies
Making the function pure in this scenario would involve removing external dependencies and ensuring that the function's output depends only on its input. This enhances testability and predictability, making the function easier to reason about and test in isolation. It also helps in avoiding side effects, which is a key characteristic of pure functions.
Can you reassign a new array or object to a variable declared with const?
- Yes, always
- Yes, but with restrictions
- No, it will result in an error
- Only if the variable is declared as a global constant
Variables declared with const cannot be reassigned to a new reference, whether it's an array, object, or any other data type. However, it's crucial to understand that this rule applies only to the reference itself, not the content of the variable. The content, if mutable (e.g., an object's properties), can still be modified.
The _________ method executes a provided function once for each array element.
- forEach
- map
- filter
- reduce
The correct answer is forEach. This method iterates over each element in an array and executes a provided function once for each element. It is commonly used for tasks like logging, updating values, or performing side effects.
Which method is used to send an AJAX request when using the Fetch API with Promises?
- fetch()
- send()
- request()
- ajax()
The fetch() method is used to send an AJAX request when utilizing the Fetch API with Promises. It is a modern and flexible API for making HTTP requests and handling responses.
Describe a scenario where using tagged template literals could be beneficial for sanitizing user input in a web application.
- They enable the creation of custom sanitization functions to process user input.
- They automatically escape special characters, preventing injection attacks.
- They provide a mechanism to enforce strict input validation rules.
- They allow for the easy integration of third-party sanitization libraries.
Tagged template literals allow developers to create custom sanitization functions, ensuring secure processing of user input and preventing potential injection attacks.