Which keyword is used to create a class in JavaScript?
- class
- new
- function
- prototype
In JavaScript, the class keyword is used to create a class. Classes provide a convenient and more structured way to define constructor functions and prototypes. While new is important for instantiating objects, it's not used to create classes. The function and prototype keywords are used in traditional constructor-based object creation.
What method can be used to select the first
element inside a
?
- div.getElementsByTagName('p')[0]
- div.querySelector('p:first-child')
- div.querySelector('p')
- div.getElementByTagName('p')
You can use div.querySelector('p') to select the first
element inside a
. This method finds the first
element that is a descendant of div. Alternatively, you can use getElementsByTagName('p')[0], but querySelector is more flexible and widely used.
What does the term "scope" refer to in JavaScript?
- The physical size of a webpage
- The lifetime of a variable
- The visibility of a variable
- The security of a webpage
In JavaScript, "scope" refers to the visibility or accessibility of a variable within a specific part of your code. It determines where in your code a particular variable can be used, which helps prevent naming conflicts and maintain code organization.
Which array method removes the first element from an array and returns that removed element?
- shift()
- unshift()
- pop()
- push()
The array method that removes the first element from an array and returns that removed element is shift(). It shifts all other elements to a lower index, effectively removing the first element. The unshift() method adds elements to the beginning, pop() removes the last element, and push() adds elements to the end of the array.
The problem of multiple nested callbacks in JavaScript is commonly referred to as ________.
- callback hell
- nested loop
- async overload
- function maze
The problem of multiple nested callbacks in JavaScript is commonly referred to as "callback hell." It occurs when callback functions are heavily nested, making the code hard to read and maintain. This can lead to readability and maintenance issues.
Which of the following methods mutates the original array?
- map()
- filter()
- concat()
- reduce()
The concat() method is used to concatenate two or more arrays and returns a new array. It does not mutate the original array. In contrast, methods like map(), filter(), and reduce() can modify the original array, so they should be used with caution.
Which type of event listener will not get removed by the removeEventListener method?
- Inline event listeners
- Anonymous event listeners
- Named function event listeners
- Arrow function event listeners
Event listeners added using named functions can be removed with the removeEventListener method because they are given a reference to the function. In contrast, inline event listeners, anonymous functions, and arrow functions are not easily removed because they lack a reference. This makes them more challenging to manage and clean up in your code.
How do you declare a variable x and assign it the value 5 in JavaScript?
- var x = 5;
- variable x = 5;
- x = 5;
- let x = 5;
To declare a variable x and assign it the value 5 in JavaScript, you use the syntax var x = 5;. This initializes the variable x with the value 5. You can also use let or const instead of var for variable declaration.
What is the potential issue with using a for...in loop to iterate over arrays?
- It throws an error.
- It iterates only over values.
- It may include inherited properties.
- It can't be used with arrays.
Using a for...in loop to iterate over arrays can lead to a potential issue because it not only iterates over the array's own properties but also includes properties that may be inherited from the prototype chain. This can result in unexpected behavior if you're not careful. To avoid this issue, it's recommended to use for...of loops when iterating over arrays or other iterable objects, as they are designed specifically for this purpose and only iterate over the values of the iterable.
What is the primary role of the setTimeout function in asynchronous JavaScript?
- Delaying code execution
- Executing code repeatedly
- Scheduling tasks
- Controlling the event loop
The primary role of the setTimeout function is to delay the execution of a piece of code for a specified amount of time (in milliseconds). It allows you to schedule a function to run after a specified delay, making it useful for creating timeouts and delays in asynchronous code.