In ES6, what happens when two different modules import the same dependency?

  • Both modules share the same instance of the dependency
  • Each module gets its own instance of the dependency
  • The first module to import the dependency "owns" it, and the second module uses that instance
  • The second module to import the dependency "owns" it, and the first module uses that instance
In ES6, each module gets its own instance of a dependency when imported. This ensures encapsulation and prevents unintended sharing of state between modules. Even if multiple modules import the same dependency, they work with independent instances, providing isolation and avoiding potential conflicts. This behavior aligns with the modular and encapsulated nature of ES6 modules, contributing to better code organization and maintainability.

What are the implications of using await in top-level code (outside of any function)?

  • Causes a syntax error
  • Works as expected
  • Results in unhandled promise rejection
  • Has no effect
Using await outside of any function (at the top level) is not allowed and results in an unhandled promise rejection. The top-level code doesn't have the necessary structure to handle asynchronous operations using await. To use await, it should be inside an async function. Otherwise, it leads to unexpected behavior and unhandled promise rejections.

What does the await keyword do inside an async function?

  • Pauses the execution of the function until the Promise is resolved
  • Executes the asynchronous code in parallel with the synchronous code
  • Skips the asynchronous code and proceeds with the synchronous execution
  • Forces the function to return immediately
The await keyword is used to pause the execution of an async function until the Promise being awaited is resolved, allowing asynchronous code to be written in a synchronous-like manner.

How does hoisting behavior differ between variables declared with let/const and those declared with var?

  • Variables declared with var are hoisted to the top of their scope, while let/const variables are hoisted but not initialized.
  • Variables declared with let/const are hoisted to the top of their scope, while var variables are hoisted but not initialized.
  • Variables declared with let/const are not hoisted, while var variables are hoisted and initialized with undefined.
  • Variables declared with var are not hoisted, while let/const variables are hoisted and initialized with undefined.
In JavaScript, variables declared with var are hoisted and initialized with undefined at the top of their scope, while let and const are hoisted but not initialized. Accessing a let/const variable before its declaration results in a ReferenceError.

In a scenario where you need to process each character of a string for a text analysis function, which loop would you choose and why?

  • for loop
  • for...in loop
  • forEach loop
  • for...of loop
The correct option is the for...of loop. This loop is specifically designed for iterating over iterable objects, such as strings, arrays, and collections. It provides direct access to the values, making it suitable for processing each character of a string. Unlike the for loop, it abstracts away the index and simplifies the code for tasks like text analysis.

What is the primary advantage of using template literals over string concatenation?

  • Enhanced Readability
  • Better Performance
  • Simplified Syntax
  • Improved Browser Compatibility
Template literals provide enhanced readability due to their concise and expressive syntax. They allow embedding variables directly within the string using ${} syntax, making the code more readable and maintainable.

Can you use computed property names in ES6 object literals?

  • Yes
  • No
  • Only in class declarations
  • Only in arrow functions
Yes, in ES6 object literals, you can use computed property names, allowing you to dynamically set property names based on variables or expressions.

What is the result of referencing this inside a static method in an ES6 class?

  • It refers to the instance of the class
  • It refers to the class itself
  • It causes a runtime error
  • It depends on the context in which the static method is called
In a static method of an ES6 class, this refers to the class itself, not to an instance of the class. Static methods are called on the class, not on instances, so they don't have access to instance-specific properties or methods.

Consider a scenario where a module is exporting multiple functions, but only some are used. How does tree shaking impact this scenario?

  • Only the used functions will be included in the final bundle, reducing its size.
  • All exported functions will be included in the bundle.
  • Tree shaking is not applicable to modules with multiple functions.
  • The entire module will be excluded from the bundle.
Tree shaking analyzes the code to include only the necessary parts. In this scenario, only the functions that are used will be included, resulting in a smaller bundle size. The other options are incorrect, as tree shaking specifically targets unused code.

When a Promise is rejected, which method is typically used to handle the rejection?

  • catch
  • finally
  • reject
  • onReject
In the context of Promises, the catch method is commonly used to handle the rejection of a Promise. It allows you to specify a callback function that will be called if the Promise is rejected, providing a way to handle errors in asynchronous operations.

How does the use of mixins and composition in ES6 aid in code reusability and maintainability?

  • By enabling the combination of behaviors from multiple sources
  • By reducing redundancy and promoting a modular approach
  • By providing a way to reuse and extend classes
  • By facilitating the creation of new classes with desired functionalities
In ES6, mixins and composition allow developers to combine functionalities from different sources, promoting code reusability. Mixins help in reducing redundancy, while composition enables the creation of modular and maintainable code.

What is a key difference in how forEach and map handle array elements?

  • forEach returns a new array, while map mutates the original array
  • forEach is synchronous, while map is asynchronous
  • forEach is purely for iteration, while map is for transformation and returns a new array
  • map modifies the original array in place, while forEach creates a new array
The key difference is that forEach is primarily for iteration and doesn't return a new array, whereas map is used for transformation and creates a new array with the results of applying a function to each element of the original array.