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.
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.
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.