Which method is preferred to loop through an array in ES6 and onwards?

  • for loop
  • for...in loop
  • forEach method
  • while loop
In ES6 and onwards, the preferred method to loop through an array is the forEach method. It provides a concise way to iterate over array elements and execute a function for each element. The for loop and for...in loop are less commonly used for iterating arrays, and the while loop is a general-purpose loop that can be used but is not specifically designed for array iteration.

How does the Factory pattern differ from the Constructor pattern in object creation?

  • Factory Pattern allows object creation without using constructors, Factory Pattern can return different object types, Factory Pattern promotes loose coupling.
  • Constructor Pattern is limited to creating objects of a specific type, Constructor Pattern enforces the use of classes, Constructor Pattern promotes tight coupling.
  • Factory Pattern encourages object creation via constructors, Factory Pattern enforces the use of classes, Factory Pattern promotes tight coupling.
  • Constructor Pattern allows object creation without using constructors, Constructor Pattern can return different object types, Constructor Pattern promotes loose coupling.
The Factory pattern allows object creation without using constructors, making it more flexible as it can return different object types. It promotes loose coupling between the client and created objects. In contrast, the Constructor pattern is limited to creating objects of a specific type, enforces the use of classes, and can lead to tight coupling.

Which of the following accurately describes how a block-scoped variable behaves outside of its block?

  • It is accessible outside the block.
  • It is undefined outside the block.
  • It retains its value outside the block.
  • It causes a syntax error if accessed outside the block.
Block-scoped variables in JavaScript are not accessible outside of their block. They have limited scope to the block they are declared in, and attempting to access them outside of that block results in an "undefined" value. This behavior helps prevent unintended variable conflicts and maintains better code isolation.

You’re working on a web application where you need to ensure that the created object removes its resources such as network connections before the creation of a new object. Which design pattern would you apply to ensure this?

  • Dispose Pattern
  • Singleton Pattern
  • Builder Pattern
  • Factory Pattern
To ensure that an object removes its resources before creating a new one, you would apply the Dispose Pattern. The Dispose Pattern is commonly used to release resources explicitly, like closing network connections, files, or database connections, when they are no longer needed. It helps prevent resource leaks and ensures efficient resource management.

How is a method in a JavaScript object typically defined?

  • Using the let keyword
  • As a variable within a function expression
  • As a named function within the object
  • By using the new keyword
A method in a JavaScript object is typically defined as a named function within the object. These functions are associated with the object and can be invoked using dot notation. Methods allow objects to encapsulate behavior, making them a key aspect of object-oriented programming in JavaScript.

Which arithmetic operator is used to calculate the remainder in JavaScript?

  • % (Modulus)
  • / (Division)
  • * (Multiplication)
  • + (Addition)
In JavaScript, the % (Modulus) operator is used to calculate the remainder of a division operation. For example, 7 % 3 equals 1 because when 7 is divided by 3, the remainder is 1.

The main difference between a function expression and a function declaration is the function _________.

  • Hoisting
  • Scope
  • Expression
  • Name
The main difference between a function expression and a function declaration is the function name. Function declarations have a name, which is used to call the function, while function expressions may or may not have a name, depending on whether they are named or anonymous functions.

Which property of the event object is commonly used to prevent the default action of the event?

  • event.stopPropagation()
  • event.preventDefault()
  • event.cancelBubble()
  • event.halt()
To prevent the default action of an event in JavaScript, you commonly use the event.preventDefault() method. It stops the default behavior associated with the event, such as preventing a form from submitting or a link from navigating to a new page. This method is crucial for controlling the behavior of events.

You have an object containing user data and need to create an array of strings containing user details in a "key: value" format. Which loop might be most suitable for this task?

  • for...in loop
  • for...of loop
  • while loop
  • forEach() method
The for...of loop is most suitable for iterating over object properties when you want to create an array of strings. It directly iterates over iterable values like arrays and works well for this task by extracting key-value pairs from the object.

Which of the following is NOT a characteristic of closures in JavaScript?

  • Data encapsulation and privacy
  • Persistence of local variables
  • Ability to access outer variables
  • Limited use of memory resources
Closures in JavaScript are powerful because they allow functions to remember and access their outer (enclosing) variables even after the outer function has finished executing. This feature can lead to memory usage if not managed properly, but it's not a limitation or disadvantage of closures per se.