What is the purpose of the .catch() method in Promise chaining?
.catch()
To handle Promise rejections (errors).
To transform the resolved value of a Promise.
To handle successful Promise resolutions.
To filter the results of a Promise.
What is a key difference between 'async/await' and traditional Promise-based code?
'async/await' is suitable for handling synchronous operations only.
'async/await' provides a more synchronous-looking syntax for asynchronous code.
'async/await' doesn't use Promises internally.
'async/await' is significantly slower than using Promises directly.
How does the 'bind()' method in JavaScript differ from 'call()' and 'apply()'?
bind() can only be used with object methods, while call() and apply() can be used with any function.
bind() creates a new function with a bound 'this' context, while call() and apply() invoke the function immediately.
bind() doesn't modify the original function, while call() and apply() do.
bind() immediately invokes the function, while call() and apply() don't.
You need to add an event listener to a dynamically created element. What technique should you use?
Directly attach the listener to the element before appending it to the DOM.
Event delegation: attach the listener to a parent element that already exists in the DOM.
Use setTimeout to delay the listener attachment until after the element is added to the DOM.
setTimeout
Attach the listener to the element's parent using event bubbling.
What does the 'Fetch API' return as the result of a successful network request?
The requested data directly as a JavaScript object.
An error object indicating a failed request.
A Promise that resolves to a Response object.
A callback function for handling the response.
What does the await keyword do inside an asynchronous function?
await
It defines a new Promise object.
It sets a timeout before executing the next line of code.
It logs an error message to the console.
It pauses execution until the Promise it's used with resolves or rejects.
What is a higher-order function in JavaScript?
A function that does not have a return statement.
A function that can only be called once.
A function that uses arrow syntax.
A function that takes another function as an argument or returns a function.
In what scenario would you typically use a callback function?
To create a loop that iterates a specific number of times.
To perform an action after an asynchronous operation completes, like fetching data from an API.
To define a constant variable.
To log data to the console for debugging purposes.
How does JavaScript's garbage collector typically identify objects that are no longer reachable?
By using a reference counting algorithm.
By using a mark-and-sweep algorithm.
By analyzing the lexical scope of variables.
By relying on manual memory deallocation.
You want to remove the third child element from a parent element with the ID 'container'. What is the correct JavaScript code?
document.getElementById('container').children[2].remove();
document.getElementById('container').childNodes[2].remove();
document.getElementById('container').deleteChild(2);
document.getElementById('container').removeChild(2);