What is the initial state of a JavaScript Promise when it is created?

  • Pending
  • Resolved
  • Rejected
  • Settled
When a Promise is created, it starts in the "Pending" state. This means that the Promise is neither fulfilled nor rejected. It's essentially in a pending state until the asynchronous operation it represents is completed.

What is the significance of weak references in WeakMap and WeakSet with regards to memory management?

  • Weak references in WeakMap and WeakSet allow for automatic garbage collection, preventing memory leaks.
  • Weak references are irrelevant in the context of WeakMap and WeakSet, as they don't impact memory management.
  • Weak references provide a way to create circular references, enhancing memory efficiency.
  • Weak references ensure that the referenced objects are not eligible for garbage collection, preserving memory.
Weak references in WeakMap and WeakSet enable automatic memory management by allowing the garbage collector to reclaim objects that are no longer reachable.

What is the primary advantage of using dynamic imports in JavaScript?

  • Code Splitting
  • Improved Code Readability
  • Faster Execution
  • Enhanced Browser Compatibility
Dynamic Imports in JavaScript allow for efficient code splitting. By loading only the modules that are needed at runtime, it improves the performance of the application, especially in large projects where not all code is required on every page.

__________ methods in ES6 classes are methods that are bound to the class itself, not to instances of the class.

  • Static
  • Instance
  • Prototype
  • Class
Static methods in ES6 classes are defined using the static keyword and are bound to the class itself rather than to instances of the class. These methods are associated with the class constructor and are called on the class, not on instances. Static methods are often used for utility functions that are related to the class but don't depend on specific instance data.

How does destructuring work when the structure of the object does not match the destructuring pattern?

  • It throws a syntax error
  • It assigns undefined to the variables
  • It ignores the mismatched values
  • It automatically corrects the structure
Destructuring in JavaScript allows extracting values from objects or arrays, and when the structure doesn't match, it simply ignores the mismatched values, assigning undefined to the corresponding variables. This behavior is useful for extracting only the needed values and ignoring the rest.

Arrow functions are not suitable for methods that need their own this context, such as ________ functions.

  • Event
  • Callback
  • Constructor
  • Recursive
Arrow functions do not have their own this context, making them unsuitable for methods that need access to the object's this value. Constructor functions, which initialize objects and rely on this, should not be implemented using arrow functions.

In ES6 Modules, imports and exports must be ________, unlike in CommonJS where they can be dynamic.

  • Explicit
  • Implicit
  • Dynamic
  • Conditional
In ES6 modules, imports and exports must be explicit, meaning they are statically determined at compile-time. This allows for better tooling support and optimizations. CommonJS, on the other hand, allows dynamic imports and exports.

Is it possible to create a shared Symbol that is accessible across different parts of your code?

  • No, Symbols are always local to the scope they are created
  • Yes, Symbols can be shared globally
  • Only if Symbols are declared as constants
  • Symbols can only be shared within the same function
Symbols can be shared globally by using the Symbol.for() method, which creates a shared Symbol registry accessible across different parts of your code. This promotes consistency and reusability.

How does the call stack in JavaScript relate to the concept of 'blocking'?

  • It represents the order of function calls
  • It handles asynchronous tasks
  • It causes delays in code execution
  • It manages the memory allocation
The call stack in JavaScript tracks the execution of functions. When a function is called, it's added to the stack, and when it returns, it's removed. Blocking occurs when a function takes a long time to execute, causing subsequent code to wait. Understanding this helps optimize code for performance.

Q1: Imagine you are creating a caching mechanism for a web application. Would you choose a WeakMap or a Map for storing cached data and why?

  • WeakMap
  • Map
  • Both WeakMap and Map
  • Neither WeakMap nor Map
In a caching mechanism, using a WeakMap is preferred over a Map. This is because a WeakMap allows the keys to be garbage collected if there are no other references to them, which is beneficial for caching scenarios where you want the cached data to be automatically cleared when the corresponding objects are no longer in use. A Map, on the other hand, would retain the keys even if they are not needed anymore, leading to potential memory leaks in a caching context.

To re-export all named exports from a module, use export * from _________.

  • export * from 'moduleName';
  • export all from 'moduleName';
  • export {all} from 'moduleName';
  • export named from 'moduleName';
To re-export all named exports from a module in ES6, you use the export * from 'moduleName'; syntax. This allows you to export all the named exports from one module to another without listing them individually.

When importing a named export, what syntax must you use?

  • import { exportName } from 'module';
  • import * as exportName from 'module';
  • import { exportName } as 'alias' from 'module';
  • import exportName from 'module';
When importing a named export in ES6, you use the syntax import { exportName } from 'module';. This allows you to bring in a specific exported item from the module.