Consider a function that fetches user data from an API. How can this function be refactored to adhere to the principles of pure functions?
- Return the fetched data directly
- Introduce a callback for data processing
- Include a random delay in fetching data
- Use global variables for storing the fetched data
To make the function pure, it should return the fetched data directly without relying on external state or global variables. Introducing a callback or using global variables can introduce side effects, which contradicts the principles of pure functions.
In a scenario where dynamic property names are based on user input, how would ES6 object literals be utilized?
- Utilize computed property names to dynamically assign properties
- Utilize the Object.assign method for dynamic property assignment
- Use Object.create for dynamic property generation
- Implement a switch statement for dynamic property naming
ES6 object literals allow the use of computed property names, which is particularly beneficial in scenarios where dynamic property names are based on user input. Computed property names enable the creation of object properties dynamically based on expressions, providing flexibility and conciseness in handling user input.
Using await inside a __________ loop can cause asynchronous operations to run sequentially.
- for
- while
- do-while
- foreach
When using await inside a loop, such as a while loop, it may lead to unexpected behavior. This is because the loop will not pause for each asynchronous operation to complete before moving to the next iteration, potentially causing them to run sequentially. It's crucial to understand the implications of using await inside loops and consider alternative approaches for parallel execution of asynchronous operations.
To use destructuring in a for...of loop iterating over a Map, you would write for (const [key, ______] of map).
- item
- val
- value
- element
To use destructuring in a for...of loop iterating over a Map, you would write for (const [key, value] of map). The value here is the second element of the destructuring assignment, representing the value associated with the current key in the Map.
How do you import a default export from a module?
- Use the import keyword followed by any name of your choice, as the default export has no specific name.
- Use the import default statement followed by the name specified in the exporting module.
- Use the import { default } from 'module'; syntax.
- Use the import * as alias from 'module'; syntax.
To import a default export from an ES6 module, you use the import keyword followed by any name of your choice, as the default export does not have a specific name. The imported name serves as an alias for the default export in your code.
Higher-order functions that alter the execution context of another function are often referred to as __________.
- Decorators
- Generators
- Closures
- Combinators
Decorators in JavaScript are higher-order functions that alter the behavior or execution context of another function. They are often used to add functionalities to functions or methods.
When exporting a class or function as a default export, it's not necessary to use the ________ keyword.
- export default
- default
- export
- class
When exporting a class or function as a default export, you can simply use export default followed by the class or function declaration without explicitly using the function or class keywords.
What happens when a generator function encounters a yield keyword?
- Pauses the function execution
- Throws an error
- Skips the yield keyword
- Resumes execution from the last yield
When a generator function encounters a yield keyword, it pauses the execution of the function, preserving its state. The function can later be resumed from the last yield encountered, allowing for efficient iteration over a sequence of values.
In functional composition, what is the result of combining two functions f and g into compose(f, g)?
- f(g(x))
- g(f(x))
- f * g
- f + g
Functional composition applies functions from right to left, so compose(f, g) means applying f after g. Therefore, the result is equivalent to f(g(x)).
Can the spread operator be used to combine two arrays into one in ES6?
- Yes
- No
- Only if the arrays are of the same length
- Only if the arrays are of different types
Yes, the spread operator can be used to combine two arrays into one by spreading the elements of both arrays into a new array. It's a concise way of merging arrays without modifying the original arrays.