What is a primary method for avoiding memory leaks in JavaScript?
Dereferencing unused objects and variables
Using 'async/await' for all asynchronous operations
Minimizing function calls
Using 'const' for all variable declarations
How does JavaScript's garbage collector typically identify objects that are no longer reachable?
By using a reference counting algorithm.
By analyzing the lexical scope of variables.
By relying on manual memory deallocation.
By using a mark-and-sweep algorithm.
You have a form with client-side validation. What is the recommended approach to prevent form submission if validation fails?
Use event.stopPropagation() to stop the submission event.
event.stopPropagation()
Use event.preventDefault() and handle the submission logic manually.
event.preventDefault()
Remove the 'submit' attribute from the form element dynamically.
Set the form's 'action' attribute to an empty string.
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').deleteChild(2);
document.getElementById('container').removeChild(2);
document.getElementById('container').children[2].remove();
document.getElementById('container').childNodes[2].remove();
What is a common practice to avoid deeply nested callbacks when working with multiple asynchronous operations?
Using nested 'try...catch' blocks extensively.
Employing synchronous functions exclusively.
Chaining multiple '.then()' methods together.
Switching to a different programming language.
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 can block the main thread, leading to a frozen UI and unresponsive interactions.
They have no significant impact on browser responsiveness, as JavaScript execution is asynchronous.
What is the primary role of the this keyword in JavaScript?
this
It refers to the current date and time.
It creates a new object instance.
It points to the object that the function is a property of, determined at function call time.
It accesses the global scope.
What is the purpose of 'event.preventDefault()' in JavaScript?
It triggers a custom event on an element.
It removes an event listener from an element.
It stops an event from being handled by any further listeners.
It prevents the default behavior of an element, like a form submission.
How can you ensure that the 'this' keyword inside a callback function refers to the intended object, especially in asynchronous operations?
By wrapping the callback function in another function.
By using the 'let' keyword to declare variables inside the callback.
By using arrow functions, which lexically inherit the 'this' value.
By invoking the callback function with 'call()' or 'apply()'.
What will be logged to the console in this code snippet: console.log((function(a) { return a * a; })(5));?
console.log((function(a) { return a * a; })(5));
undefined
25
10
5