In JavaScript, the arithmetic operator _______ is used to exponentiate a number.

  • **
  • ^
  • exp
  • pow
In JavaScript, the ** operator is used to exponentiate a number. For example, 2 ** 3 evaluates to 8, as it calculates 2 raised to the power of 3. This operator was introduced in ECMAScript 2016 (ES6) to provide a concise way of performing exponentiation.

How does the "this" keyword behave in arrow functions compared to regular functions?

  • It refers to the global object.
  • It retains the value of "this" from its enclosing lexical context.
  • It becomes undefined.
  • It refers to the parent function's "this".
In arrow functions, the value of "this" is determined by its enclosing lexical context (the function that contains it). This behavior is different from regular functions, where "this" is dynamically scoped and can change based on how the function is called. This makes arrow functions particularly useful for maintaining the expected value of "this" in functions defined within methods or callbacks.

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.

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.