What is the result of the expression NaN == NaN in JavaScript?

  • TRUE
  • FALSE
  • NaN
  • Throws an error
In JavaScript, NaN is not equal to itself. So, the expression NaN == NaN evaluates to false. This behavior is because NaN represents a value that is "Not-a-Number," and it's treated as unordered and not equal to any other value, including itself.

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.

What is a key difference between arrow functions and regular functions in JavaScript?

  • Arrow functions have their own 'this' context
  • Regular functions are more concise
  • Arrow functions cannot have parameters
  • Regular functions do not allow for anonymous functions
One significant difference between arrow functions and regular functions is that arrow functions do not have their own 'this' context; instead, they inherit 'this' from the surrounding code. Regular functions, on the other hand, have their own 'this' context, which can vary depending on how the function is called.

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.