How can functional composition benefit the readability and maintainability of JavaScript code?
- Enhances code modularity
- Reduces side effects
- Encourages reuse of functions
- All of the above
Functional composition involves combining small, pure functions to create more complex ones, enhancing modularity, reducing side effects, and promoting code reuse. This leads to improved code readability and maintainability.
When using Promises with AJAX, handling network errors is done through the ______ method of the Fetch API.
- error
- fail
- catch
- reject
When using Promises with AJAX, handling network errors is done through the catch method of the Fetch API. The catch block allows you to handle any errors that occurred during the AJAX request.
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.
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.
ES6 introduces the __________ property in object literals for setting the prototype of the created object.
- Prototype
- proto
- Object.setPrototypeOf()
- Object.create()
In ES6, the proto property in object literals is used for setting the prototype of the created object. It provides a convenient way to establish the prototype chain for an object.
What happens if an error is thrown inside a .then() block in Promise chaining?
- It is automatically caught by the nearest .catch() block in the chain.
- The program crashes with an unhandled exception.
- The error is ignored, and the program continues execution.
- It triggers the global error handler.
If an error occurs inside a .then() block, it will be caught by the nearest .catch() block in the promise chain, preventing it from propagating further and allowing proper error handling.
What is the purpose of the prototype chain in JavaScript?
- To store local variables
- To manage asynchronous operations
- To enable inheritance and method sharing
- To restrict access to certain properties
The prototype chain in JavaScript is crucial for inheritance. When an object is created, it inherits properties and methods from its prototype. This chain allows objects to share functionality and promotes code reusability.