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.

What is the significant difference between Java and JavaScript?

  • Java is primarily a client-side language
  • JavaScript is a compiled language
  • Java is a strongly typed language
  • JavaScript is primarily used for web scripting
The significant difference between Java and JavaScript lies in their purpose and usage. Java is primarily a server-side language, whereas JavaScript is used for client-side web scripting. Additionally, Java is a statically typed language, while JavaScript is dynamically typed, which means variable types are determined at runtime. Understanding these differences is essential when choosing the right language for a particular task.

How does JavaScript handle non-strict equality (==) comparisons in switch statements?

  • It uses strict equality (===) for comparisons.
  • It converts both the expression and case values to the same type before comparing.
  • It evaluates the expression and case values as-is without any type conversion.
  • It throws an error for non-strict equality comparisons.
JavaScript uses non-strict (loose) equality (==) comparisons in switch statements. It evaluates the expression and case values without any type conversion. This means that values with different types may be considered equal if their "abstract equality" holds true. For example, '1' == 1 is true.

During a code review, you spot the splice() method being used to remove an element from the end of an array. What alternative method might you suggest for removing elements from the end of an array that might be more performant and simpler?

  • pop()
  • shift()
  • slice()
  • push()
To remove an element from the end of an array, it's more efficient and simpler to use the pop() method. The pop() method removes the last element from the array and returns it, ensuring O(1) time complexity, whereas splice() is unnecessary for this task and can be less efficient.

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.

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.