Object literals in ES6 can include __________, a shorthand for defining functions.
- Arrow Functions
- Prototypes
- Generators
- Callbacks
ES6 introduced arrow functions as a shorthand for defining functions within object literals, offering a more concise syntax and lexically scoped 'this'.
What happens when you use the return keyword in a single-line arrow function?
- The function returns undefined
- The function returns the value after the arrow
- The function throws an error
- The function returns an array
In arrow functions, if the body is a single expression, it is implicitly returned. So, using the return keyword is optional. Option B is correct.
__________ can be used to execute a callback on every element in the array, creating a new array with the results.
- map
- forEach
- reduce
- filter
The forEach method can be used to execute a callback function on each element of an array. However, it doesn't create a new array with the results.
Generator functions can be used to implement _________ programming patterns, like handling asynchronous operations in a synchronous manner.
- Synchronous
- Asynchronous
- Parallel
- Concurrent
Generator functions in JavaScript can be used to implement asynchronous programming patterns. By yielding promises and using async/await syntax, asynchronous operations can be handled in a more synchronous-looking manner, enhancing code readability and maintainability.
How are mixins typically applied to a class in ES6?
- Using the extends keyword
- Using the include keyword
- Using the mixin keyword
- By manually copying and pasting the code
Mixins are typically applied to a class in ES6 using the extends keyword. This establishes a relationship between the class and the mixin, allowing the class to inherit the properties and methods defined in the mixin. This method of composition is clean and maintains a clear structure in the code.
Unlike callbacks, Promises support _______ chaining, which helps in writing cleaner code.
- callback
- asynchronous
- promise
- then
The correct option is 'then.' Promises support 'then' chaining, allowing you to chain multiple asynchronous operations in a more readable and maintainable way. This is a significant improvement over callbacks, leading to cleaner and more structured code.
When chaining promises, the return value of one .then() becomes the input for the next _________.
- catch()
- resolve()
- reject()
- then()
The correct term is then(). When chaining promises, the then() method is used to specify what should happen next after the Promise is fulfilled. The value returned by the then() callback becomes the input for the next then() in the chain.
When using const with destructuring, you must provide a _________ at the time of declaration.
- Value
- Reference
- Type
- Default Value
When using const with destructuring, you must provide a value at the time of declaration. This is because, once a variable is declared with const, you cannot reassign it later.
Can await be used inside a regular (non-async) function?
- No, it can only be used in async functions
- Yes, it can be used to make any function asynchronous
- Yes, but it will result in a runtime error
- No, it is only applicable in arrow functions
The await keyword can only be used inside functions marked with the async keyword. Attempting to use await in a regular function will result in a syntax error.
In ES6, which scenario would necessitate using let over const?
- When the variable needs to be reassigned
- When the variable should remain constant throughout its scope
- When the variable is used in a loop
- When the variable is a function declaration
The 'let' keyword should be used when the variable needs to be reassigned within its scope, as 'const' variables cannot be reassigned after initialization. 'const' is appropriate for variables that should remain constant throughout their scope.