In what scenario would you typically use a callback function?
To log data to the console for debugging purposes.
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.
How do modules enhance code organization and reusability in JavaScript?
They are used for creating user interfaces and handling user interactions.
Modules enable asynchronous programming, improving the performance of web applications.
They allow developers to group related code into separate files and control their visibility and accessibility, reducing global scope pollution.
Modules primarily handle data persistence in JavaScript applications.
Which method is most efficient for finding a specific element in the DOM based on its ID?
getElementById('my-id')
getElementsByClassName('my-class')[0]
querySelectorAll('#my-id')[0]
querySelector('#my-id')
Which built-in JavaScript method allows you to pass an array of arguments to a function, effectively spreading them as individual arguments?
bind()
spread()
call()
apply()
What is the purpose of the response.json() method when using the Fetch API?
response.json()
It sets the Content-Type header of the request to 'application/json'.
It parses the response body as JSON data.
It converts a JavaScript object into a JSON string.
It sends a JSON-encoded request to the server.
What is the primary purpose of constructor functions in JavaScript?
To define static methods for a class.
To define private variables within a function scope.
To create multiple instances of an object with shared properties and methods.
To handle asynchronous operations.
You need to add an event listener to a dynamically created element. What technique should you use?
Event delegation: attach the listener to a parent element that already exists in the DOM.
Attach the listener to the element's parent using event bubbling.
Directly attach the listener to the element before appending it to the DOM.
Use setTimeout to delay the listener attachment until after the element is added to the DOM.
setTimeout
What is a primary method for avoiding memory leaks in JavaScript?
Using 'const' for all variable declarations
Using 'async/await' for all asynchronous operations
Minimizing function calls
Dereferencing unused objects and variables
How can you ensure that a piece of code runs only after a Promise has been rejected?
By using the '.then()' method.
By using the 'setTimeout()' function.
By using the '.catch()' method.
By placing the code immediately after the Promise is created.
What is the purpose of the .catch() method in Promise chaining?
.catch()
To handle successful Promise resolutions.
To handle Promise rejections (errors).
To filter the results of a Promise.
To transform the resolved value of a Promise.