What does the prototype property of a function allow you to do?
- a) Create a new instance of the function.
- b) Add new properties and methods to all instances created by that function.
- c) Access the parent prototype of the function.
- d) Change the function's name and scope.
The prototype property of a function in JavaScript allows you to add new properties and methods to all instances created by that function. This is useful for implementing inheritance and sharing common behavior among objects created from the same constructor function. It doesn't create new instances or change the function's name or scope.
How do you declare a two-dimensional array in JavaScript?
- let arr = [[]];
- let arr = [][[]];
- let arr = [[], []];
- let arr = [[],] [[]];
In JavaScript, a two-dimensional array is declared by creating an array where each element is another array. This creates a matrix-like structure. For example, let arr = [[], []]; declares a 2x2 two-dimensional array. The other options contain syntax errors or do not create a valid two-dimensional array.
You are working with a NodeList after querying the DOM and want to iterate over each node. Which loop would be directly usable without converting the NodeList to an array?
- for...in loop
- for...of loop
- while loop
- NodeList.prototype.forEach() method
The NodeList is not an array, but you can use the NodeList.prototype.forEach() method directly to iterate over its nodes without converting it to an array. It provides a convenient way to work with NodeLists, making it the suitable choice.
The _______ pattern is used to create an instance of an object with some default values.
- Factory
- Prototype
- Singleton
- Observer
The Factory pattern is used to create an instance of an object with some default values. It provides an interface for creating objects in a super factory or a factory method. This pattern is commonly used in JavaScript to create objects.
When a function expression is made async, it returns a ______.
- Promise
- Callback
- Generator
- Class
When a function expression is marked as "async," it returns a Promise. An async function can pause its execution and wait for the Promise to resolve or reject, which is helpful for handling asynchronous operations elegantly.
A do-while loop is particularly useful when you want to ensure the loop body executes at least ________ before checking the condition.
- Once
- Twice
- Never
- Once or more
A do-while loop is useful when you want to guarantee that the loop body executes at least "once" before checking the condition. This is because in a do-while loop, the condition is evaluated after the loop body, ensuring that the code inside the loop executes at least once, even if the condition is initially false.
You're troubleshooting a web application and notice that an event is not being captured during the capturing phase as expected. What could be a potential reason for this event not being captured?
- The event is not properly attached
- The event is not registered
- The event is being canceled by another event
- The event handler is asynchronous
One potential reason for an event not being captured during the capturing phase is that the event is not properly attached to the target element. Capturing phase events need to be registered correctly using methods like addEventListener with the true parameter to enable the capturing phase. Other options may affect event handling but are not specific to the capturing phase.
To avoid cross-site scripting (XSS) attacks, instead of innerHTML, use _________.
- textContent
- appendChild
- createElement
- setAttribute
To prevent XSS attacks, it's recommended to use the textContent property. Unlike innerHTML, which can execute scripts, textContent only sets the text content of an element, making it safer for user-generated input.
The method _________ is used to parse a JSON response in Fetch API.
- .json()
- .parseJSON()
- .stringify()
- .fromJSON()
In the Fetch API, the .json() method is used to parse a JSON response. It takes the JSON response and returns a JavaScript object that you can work with in your code, making it easier to extract and use the data.
What is the primary difference between a class and an instance?
- A class is a blueprint for objects
- An instance is a prototype for classes
- A class is an object
- An instance is a collection of methods
The primary difference between a class and an instance in JavaScript is that a class serves as a blueprint or template for creating objects, while an instance is a specific object created from that class. Classes define the structure and behavior of objects, while instances are actual objects with their unique data and state.