If a module exports several named items, how can you import all of them at once?

  • import * as allExports from 'module';
  • import { export1, export2 } from 'module';
  • import allExports from 'module';
  • import { * } from 'module';
To import all named exports from a module at once, you use the syntax import * as allExports from 'module';. This creates an object containing all the exports, and you can access them using dot notation, e.g., allExports.exportName.

What is a key difference between a Map and a WeakMap in JavaScript?

  • Both can store key-value pairs, but the keys in WeakMap must be objects, and they don't prevent the objects from being garbage collected.
  • Maps allow any data type as keys, while WeakMaps only allow objects as keys.
  • Maps are iterable, while WeakMaps are not iterable.
  • WeakMaps have a getKeys method to retrieve all keys.
Maps are versatile and allow various data types as keys, while WeakMaps are designed for enhanced privacy, with keys limited to objects and no direct method for key retrieval.

In a node.js application, how would you handle errors when performing multiple asynchronous operations in parallel?

  • Implementing a try-catch block around each asynchronous operation
  • Utilizing the Promise.all() method with a single catch block for error handling
  • Using multiple catch blocks for each asynchronous operation
  • Handling errors outside the asynchronous operations entirely
In the scenario of multiple asynchronous operations, using Promise.all() simplifies error handling by allowing a single catch block to capture errors from any of the parallel operations. This promotes cleaner and more concise error management, making it easier to identify and address issues in the asynchronous flow. Multiple catch blocks might lead to redundancy and make the code harder to maintain.

In the context of tree shaking, what is the significance of the "sideEffects" flag in a package.json file?

  • Specifies files that should be excluded from the tree-shaking process
  • Indicates whether the package has side effects that prevent tree shaking
  • Determines the priority of modules during tree shaking
  • Flags external dependencies for tree shaking
The "sideEffects" flag in package.json is used to inform the bundler about the side effects of a module. If set to false, it allows the bundler to perform aggressive tree shaking by eliminating unused exports. If set to true, it implies the module has side effects, and the bundler avoids tree shaking to maintain those side effects.

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.

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.

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.

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.

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.

Consider a library that needs to add unique identifiers to objects without risk of property clashes. How would Symbols be utilized in this case?

  • Using string identifiers
  • Using Symbols as unique identifiers
  • Using numbers as identifiers
  • Using object literals with unique keys
The correct option is using Symbols as unique identifiers. Symbols provide a way to create unique keys for object properties, ensuring that the identifiers added by the library are distinct and not prone to clashes. Using string identifiers or numbers may lead to conflicts, especially in a shared library context. Object literals with unique keys can be used, but Symbols offer a more direct and elegant solution.