What does the pop() method do to a JavaScript array?
- Removes the last element
- Removes the first element
- Adds an element to the end
- Adds an element to the beginning
The pop() method in JavaScript removes the last element from an array and returns that element. This operation reduces the length of the array by 1. It's the opposite of the push() method, which adds an element to the end of the array. pop() is useful for removing elements from the end of a stack-like structure.
The _________ method of an XMLHttpRequest object is used to send the request to a server.
- request()
- open()
- send()
- fetch()
The correct method to send a request to a server using an XMLHttpRequest object is the send() method. It sends the request to the specified URL, initiating the request to the server. The open() method is used to set up the request, while fetch() is used with the Fetch API.
Which of the following options is a technique to manage callback hell in JavaScript?
- Promises
- Event emitters
- Error handling strategies
- Callback chaining
Promises are a technique in JavaScript to manage callback hell. They provide a more structured and readable way to handle asynchronous operations, allowing you to chain multiple asynchronous calls together, making the code cleaner and easier to understand.
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.
A common use-case for a "for" loop in asynchronous programming is to use it with the ________ function.
- setTimeout
- setInterval
- async/await
- Promise
A common use-case for a "for" loop in asynchronous programming is to use it with the Promise function. Promises are often used to handle asynchronous operations in JavaScript, allowing you to work with asynchronous data in a more structured manner, making it a crucial part of modern JavaScript development.
The Fetch API returns a _________ which resolves to the Response of the request, whether it is successful or not.
- Promise
- Callback
- Array
- Event
The Fetch API returns a Promise, which allows you to handle asynchronous operations. This Promise resolves to the Response object of the request, containing information about the request and its status, whether it succeeds or not.
Which method is used to attach an event listener to an element in JavaScript?
- addListener()
- addEventListener()
- attachEvent()
- addEventHandler()
In JavaScript, the correct method to attach an event listener to an element is addEventListener(). This method allows you to specify the event type to listen for and the function to execute when the event occurs. It is the modern and recommended way to handle events in JavaScript.
In JavaScript, you can add a new property to an object by simply assigning a value to it with the _________ operator.
- add
- extend
- assign
- new
In JavaScript, you can add a new property to an object by simply assigning a value to it with the assign operator. The assign operator is used to add or update properties on an object. It's a common operation for dynamically working with objects and their properties in JavaScript.