What is a common way to achieve encapsulation in JavaScript when modules are not an option (e.g., in older environments)?
Relying solely on comments to indicate private members, as there are no built-in mechanisms for true privacy.
Using global variables to store private data.
Encapsulation is not achievable without modules in JavaScript.
Leveraging closures to create private scopes and control access to variables and functions within those scopes.
Consider the code: const myFunc = (a, b) => a + b;. What type of function is myFunc?
const myFunc = (a, b) => a + b;
myFunc
Arrow function
Generator function
Method
Constructor function
How does JavaScript's garbage collector typically identify objects that are no longer reachable?
By using a mark-and-sweep algorithm.
By using a reference counting algorithm.
By relying on manual memory deallocation.
By analyzing the lexical scope of variables.
You have a form with client-side validation. What is the recommended approach to prevent form submission if validation fails?
Remove the 'submit' attribute from the form element dynamically.
Set the form's 'action' attribute to an empty string.
Use event.preventDefault() and handle the submission logic manually.
event.preventDefault()
Use event.stopPropagation() to stop the submission event.
event.stopPropagation()
What is a common practice to avoid deeply nested callbacks when working with multiple asynchronous operations?
Switching to a different programming language.
Employing synchronous functions exclusively.
Using nested 'try...catch' blocks extensively.
Chaining multiple '.then()' methods together.
How does the 'bind()' method in JavaScript differ from 'call()' and 'apply()'?
bind() creates a new function with a bound 'this' context, while call() and apply() invoke the function immediately.
bind() immediately invokes the function, while call() and apply() don't.
bind() can only be used with object methods, while call() and apply() can be used with any function.
bind() doesn't modify the original function, while call() and apply() do.
What is a primary method for avoiding memory leaks in JavaScript?
Using 'async/await' for all asynchronous operations
Using 'const' for all variable declarations
Minimizing function calls
Dereferencing unused objects and variables
What is a higher-order function in JavaScript?
A function that does not have a return statement.
A function that uses arrow syntax.
A function that can only be called once.
A function that takes another function as an argument or returns a function.
What is the purpose of the .catch() method in Promise chaining?
.catch()
To handle Promise rejections (errors).
To filter the results of a Promise.
To transform the resolved value of a Promise.
To handle successful Promise resolutions.
How can you ensure that the 'this' keyword inside a callback function refers to the intended object, especially in asynchronous operations?
By using the 'let' keyword to declare variables inside the callback.
By wrapping the callback function in another function.
By using arrow functions, which lexically inherit the 'this' value.
By invoking the callback function with 'call()' or 'apply()'.