How do ES6 classes simplify object-oriented programming in JavaScript compared to constructor functions?
Classes eliminate the need for prototypes, making object creation more efficient.
ES6 classes are primarily used for working with the DOM and have limited object-oriented capabilities.
ES6 classes introduce a completely new inheritance model unrelated to prototypes.
Classes offer a more concise and familiar syntax for defining objects and inheritance, though they still utilize prototypes under the hood.
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.
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
Attach the listener to the element's parent using event bubbling.
How can you handle errors inside an async function?
async
By calling the .catch() method on the async function itself.
.catch()
Using a try...catch block.
try...catch
Errors cannot be handled inside async functions.
By using the throw keyword.
throw
Which of these methods allows you to explicitly set the this value when calling a function in JavaScript?
this
.filter()
.forEach()
.map()
.call()
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').removeChild(2);
document.getElementById('container').childNodes[2].remove();
document.getElementById('container').deleteChild(2);
document.getElementById('container').children[2].remove();
How does JavaScript's garbage collector typically identify objects that are no longer reachable?
By using a mark-and-sweep algorithm.
By analyzing the lexical scope of variables.
By using a reference counting algorithm.
By relying on manual memory deallocation.
What is the impact of long-running JavaScript tasks on the browser's responsiveness?
They improve rendering performance by allowing the browser to optimize resource allocation.
They enhance user experience by providing smoother animations and transitions.
They have no significant impact on browser responsiveness, as JavaScript execution is asynchronous.
They can block the main thread, leading to a frozen UI and unresponsive interactions.
What is the primary role of the this keyword in JavaScript?
It accesses the global scope.
It creates a new object instance.
It refers to the current date and time.
It points to the object that the function is a property of, determined at function call time.
In the context of ES6 classes, what is the purpose of the 'extends' keyword?
It extends the functionality of a built-in JavaScript object like Array or String.
It's used to create an instance of a parent class.
It defines a new static method within a class.
It establishes inheritance by creating a subclass that inherits properties and methods from a superclass.
What is a primary method for avoiding memory leaks in JavaScript?
Using 'const' for all variable declarations
Minimizing function calls
Dereferencing unused objects and variables
Using 'async/await' for all asynchronous operations