When a function is defined inside another function and has access to the outer function’s variables, the inner function is known as a _________.

  • Nested Function
  • Inner Function
  • Callback Function
  • Closure
A closure is a JavaScript feature where a function has access to variables from its containing (enclosing) function's scope even after the outer function has finished executing. It's a powerful concept for maintaining data privacy and creating functions that remember and can access their outer scope.

What is NOT a consequence of dynamic scoping, considering JavaScript uses lexical scoping?

  • Variables can change unexpectedly
  • Function definitions capture their context
  • Easier to reason about
  • Potential bugs due to unexpected changes
In JavaScript, dynamic scoping is not a consequence since JavaScript primarily uses lexical scoping. With lexical scoping, variable scope is determined by the placement of variables in the source code, making it easier to reason about the code's behavior. Dynamic scoping, where scope is determined by the calling context at runtime, can lead to unexpected changes in variable values and potential bugs, but it is not a consequence of JavaScript's lexical scoping.

How can a "for" loop be used for asynchronous operations efficiently?

  • By using "await" within the loop body
  • By using a "setTimeout" function inside the loop
  • By using "return" statements in the loop
  • By using "if...else" conditions in the loop
To use a "for" loop for asynchronous operations, you can use the "await" keyword within the loop body, making it an "async" function. This allows you to wait for asynchronous tasks to complete in each iteration, ensuring that the loop proceeds in an orderly fashion. The other options are not suitable for efficient asynchronous operations.

How can you modify the behavior of for...of loops with iterables?

  • By using the for...in loop instead.
  • By providing a custom iterator object.
  • By using Array.prototype.forEach() method.
  • By changing the loop syntax (e.g., for...to).
You can modify the behavior of for...of loops with iterables by providing a custom iterator object. Iterables in JavaScript have an @@iterator method that defines how the iteration should behave. By implementing this method, you can customize how for...of iterates over your objects.

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.

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.