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.
What is the primary benefit of using Promises in AJAX calls over traditional callback functions?
- Asynchronous handling
- Synchronous handling
- Simplicity
- Compatibility
Promises provide a cleaner and more readable way to handle asynchronous operations, making code more maintainable and avoiding callback hell. They allow better error handling and chaining of multiple asynchronous operations.
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.
Given a large project that requires conditional loading of modules based on runtime conditions, which module system would you prefer, ES6 Modules or CommonJS, and why?
- ES6 Modules
- CommonJS
- Both are equally suitable
- Depends on the project requirements
In a large project with conditional module loading, ES6 Modules offer advantages such as static analysis during the build process, enabling more efficient tree shaking and minimizing unused code. CommonJS, being synchronous, might introduce delays in loading modules, making it less suitable for dynamic scenarios.
What is the main difference between higher-order functions and callbacks?
- Higher-order functions take functions as arguments, while callbacks are functions passed as arguments.
- Callbacks are used only in asynchronous operations.
- Higher-order functions always return a value.
- Callbacks can only be anonymous functions.
Higher-order functions are functions that take other functions as arguments, offering flexibility, while callbacks are simply functions passed as arguments to other functions.
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.