To ensure all errors are caught in an async function, use try/catch along with ________.

  • async/await
  • catch/error
  • reject/resolve
  • throw/catch
When working with asynchronous code in JavaScript, using try/catch blocks along with async/await ensures that errors within the async function are properly caught and handled.

Named exports are useful for ___________ functionality across modules.

  • hiding
  • sharing
  • encapsulating
  • extending
Named exports in ES6 allow you to share specific functionalities across modules. It is a way to expose selected parts of a module to be used in other modules, making option b) sharing the correct answer.

The ability to _________ values during destructuring allows for more flexible data manipulation.

  • skip
  • swap
  • default
  • mutate
The ability to set default values during destructuring provides flexibility in handling undefined or missing values, enhancing data manipulation capabilities.

Side effects in a function include things like modifying a _________ or logging to the console.

  • Variable
  • Object
  • Array
  • State
Side effects can include modifying external state, like changing the value of a variable or an object, which goes against the principles of pure functions.

Using the rest operator in function parameters collects the rest of the arguments into an ________.

  • Object
  • Array
  • Iterable
  • String
The rest operator (...) in function parameters is used to collect the rest of the arguments into an array. It allows a function to accept a variable number of arguments.

For a function that calculates a discount on a price, how can default parameters be used to make the function adaptable to different discount strategies?

  • Use default parameters to set a default discount strategy, like calculateDiscount(price, discount = 0.1).
  • Allow users to pass a discount strategy as a parameter and use default parameters to handle cases where no strategy is provided.
  • Utilize default parameters for individual discount components, such as percentage and fixed amount, offering flexibility in discount calculation.
  • Set default parameters for both price and discount strategy, allowing users to calculate discounts with a default strategy or a custom one.
Default parameters can enhance the adaptability of a discount calculation function. By setting a default discount strategy, users have the option to either accept the default or provide a custom strategy. This design allows for a default behavior while accommodating different discount strategies based on user preferences or specific use cases.

What is the impact of using a pure function on the predictability and testability of code?

  • Increases unpredictability
  • Decreases testability
  • Enhances predictability
  • No impact on testability
Using pure functions in code enhances predictability by ensuring that the function's output is solely determined by its input, without relying on external state. This predictability simplifies testing, as pure functions can be easily isolated and tested independently, contributing to overall code reliability.

How does Promise chaining help in handling asynchronous operations?

  • It simplifies callback hell by allowing sequential execution of asynchronous tasks.
  • It increases the complexity of asynchronous code.
  • It only works with synchronous operations.
  • It requires additional external libraries.
Promise chaining is a technique that allows sequential execution of asynchronous tasks, making the code more readable and maintainable by avoiding callback hell.

What happens if a method is called on an object that does not define it but its prototype does?

  • The method will throw an error
  • The method will be executed using the prototype's implementation
  • The method will be executed using the object's implementation
  • The method will call the constructor of the object
If a method is called on an object that does not define it but its prototype does, the method will be executed using the prototype's implementation. This is a key feature of prototype-based inheritance in JavaScript.

The __________ loop is used to iterate over the elements of an iterable object in JavaScript.

  • for...in
  • while
  • do...while
  • for...of
The for...of loop is specifically designed for iterating over the values of an iterable object, providing a concise and readable syntax for iteration. It simplifies the process of iterating over arrays, strings, maps, sets, and other iterable objects.