Consider a module that exports multiple utility functions. How would you import a specific function for optimizing bundle size?

  • Importing the entire module and using the specific utility function
  • Destructuring the import statement to only include the needed utility function
  • Importing the module dynamically at runtime based on the required utility function
  • Using a wildcard import to import all utility functions
To optimize bundle size, it's advisable to import only the specific utility function needed rather than importing the entire module. Destructuring the import statement allows you to selectively import only the required function, reducing unnecessary code and improving performance.

To prevent infinite recursion, it is important to define a __________ that stops the recursion.

  • Base case
  • Recursive case
  • Break statement
  • Counter variable
In recursive functions, a base case is crucial to terminate the recursion. It is the condition that, when met, stops the function from calling itself endlessly.

In the map method, what happens if the callback function doesn't return any value?

  • It throws an error
  • It returns undefined for each element
  • It skips the current element in the resulting array
  • It returns an array of null values
If the callback function in the map method doesn't return any value (implicitly or explicitly), each corresponding element in the resulting array will have the value undefined. It's essential to ensure that the callback function produces meaningful output to avoid unexpected results.

What is the significance of side effects in a function in terms of functional programming?

  • Enhances performance
  • Allows for state changes outside the function
  • Simplifies code structure
  • Ensures security
Side effects, such as changing external state, are discouraged in functional programming as they make it harder to reason about the behavior of a function and can lead to unexpected issues.

To override a static method in a subclass, you need to use the same method name and the _________ keyword.

  • super
  • override
  • static
  • extend
To override a static method in a subclass, you use the same method name as the parent class and the super keyword. This allows you to call the parent class's static method while providing a new implementation in the subclass.

What happens if a class in ES6 does not have a constructor?

  • It throws an error
  • It automatically creates an empty constructor
  • It inherits the constructor from the parent class
  • It is not possible to have a class without a constructor
In ES6, if a class does not have a constructor, a default constructor is automatically created. This default constructor is empty and does nothing. This behavior helps in cases where a class does not require any specific initialization logic in its constructor.

If you are creating a function to navigate through a tree-like data structure, how would recursion aid in this process?

  • Recursively call the function for each child node, allowing traversal through the entire tree.
  • Utilize a for loop to iterate over each level of the tree structure.
  • Apply the Array.prototype.map method to map through the tree nodes.
  • Use the Array.prototype.filter method to filter out unwanted nodes in the tree.
Recursion is instrumental in navigating a tree-like data structure by allowing the function to traverse through each level of the tree effortlessly. The recursive call on each child node ensures a thorough exploration of the entire structure. Other options lack the inherent nature of recursion for tree traversal.

How does error handling differ in async/await compared to traditional promise-based approaches?

  • Both handle errors in the same way
  • Async/await uses try...catch for error handling
  • Promises use try...catch for error handling
  • Async/await relies on callback functions
In async/await, errors are handled using the familiar try...catch block, making it easier to write and read asynchronous code. This improves the debugging experience compared to chaining .then() and .catch() in promise chains.

What happens when you add duplicate elements to a Set?

  • Ignored, Overwritten, Error, Concatenated
  • Overwritten, Ignored, Concatenated, Error
  • Concatenated, Overwritten, Ignored, Error
  • Ignored, Concatenated, Error, Overwritten
When you add duplicate elements to a Set in JavaScript, they are ignored. Unlike Arrays or Objects, Sets only allow unique values, ensuring that the Set contains only distinct elements. Adding a duplicate element does not result in an error, and the Set remains unchanged.

In a WeakMap, keys must be of type _________, and they are not enumerable.

  • Object
  • String
  • Symbol
  • Number
In a WeakMap, keys must be of type Symbol, and they are not enumerable. Symbols are unique and help in creating private properties or methods. Using other types as keys will result in unexpected behavior.