A pure function always returns the same output given the same _________.

  • Input
  • Output
  • Variable
  • Function
A pure function is a function where the output is solely determined by its input, making it predictable and easier to test. It always returns the same output for the same input.

How does the event loop handle callback functions in JavaScript?

  • It executes callbacks immediately when encountered
  • It adds callbacks to the call stack for execution
  • It delegates callbacks to the message queue for later execution
  • It ignores callbacks entirely
The event loop in JavaScript works by constantly checking the call stack and message queue. When the call stack is empty, it picks up tasks from the message queue, including callback functions, and adds them to the call stack for execution. This ensures asynchronous operations are handled in a non-blocking way.

Given a scenario where you need to flatten a deeply nested array, how would you implement this using recursion in ES6?

  • Use Array.prototype.concat with recursion to flatten the array.
  • Use a for...of loop to iterate through the array and flatten it.
  • Implement a reduce function with recursion to flatten the array.
  • Utilize the Array.prototype.flat method available in ES6.
In ES6, the reduce function can be leveraged along with recursion to flatten a deeply nested array effectively. The accumulator in reduce helps to accumulate the flattened result. This approach is versatile and applicable to various array structures. The other options either lack the necessary recursion or use methods not specifically designed for flattening deeply nested arrays.

How does the super keyword differ when used in static methods compared to non-static methods?

  • It behaves the same in both static and non-static methods
  • It is not allowed in static methods
  • It refers to the superclass in non-static and subclass in static methods
  • It is used to invoke the constructor of the superclass in both cases
In a static method, 'super' refers to the parent class, while in a non-static method, it points to the parent class. This is because static methods are called on the class itself, and non-static methods are called on instances of the class.

In ES6, how can you define a method inside an object literal?

  • Function shorthand notation
  • Method shorthand notation
  • Prototype method notation
  • Arrow function notation
In ES6 object literals, you can define a method using the method shorthand notation. This provides a concise way to declare functions as properties of an object.

Which method would you choose to execute a side effect for each array element without modifying the array?

  • forEach
  • map
  • filter
  • reduce
The forEach method is designed for executing a provided function once for each array element without modifying the array itself. It is commonly used when you need to perform side effects, such as logging or updating external variables, for each element.

Can the super keyword be used in a class that does not extend another class?

  • Yes
  • No
  • Only in static methods
  • Only in arrow functions
No, the super keyword is used to call corresponding methods of the superclass in a subclass. If a class doesn't extend another class, there is no superclass, and using super is not valid.

In a recursive function, how can default parameters be used to track state across recursive calls?

  • Utilize default parameter values to maintain state
  • Use default parameters only for base cases
  • Default parameters are not suitable for recursive functions
  • Recursive functions cannot use default parameters
In a recursive function, default parameters can be leveraged to maintain state across different recursive calls. This allows you to pass and update values without explicitly passing them in each recursive call. For example, consider a factorial function where the default parameter keeps track of the current product.

How do Symbols contribute to better property encapsulation in objects?

  • Symbols allow for private properties
  • Symbols allow for dynamic property names
  • Symbols enforce strict encapsulation
  • Symbols are interchangeable with property names
Symbols in JavaScript can be used as unique keys for object properties. This allows for the creation of private properties and better encapsulation, as Symbols are not enumerable in for...in loops and do not clash with other property names.

How do generator functions differ from traditional functions in terms of execution?

  • Generator functions have multiple entry points
  • Generator functions always return a value
  • Generator functions are synchronous
  • Generator functions have the ability to pause and resume execution
Generator functions differ from traditional functions by having the ability to pause and resume their execution using the yield keyword. This feature enables the creation of iterators and asynchronous programming patterns. Traditional functions, on the other hand, run to completion without the ability to pause.