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 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.
How does hoisting behave in function declarations in JavaScript?
- Function declarations are moved to the top of their containing scope during compilation.
- Function declarations are not affected by hoisting.
- Hoisting only applies to variables, not functions.
- Function declarations are moved to the bottom of the code.
In JavaScript, hoisting is the mechanism by which variable and function declarations are moved to the top of their containing scope during compilation. This means that you can call a function declared with function before it appears in your code, and it will still work. However, it's important to note that only the declarations are hoisted, not the initializations. Understanding hoisting is crucial for writing clean and maintainable JavaScript code.
A _________ object is used to perform HTTP requests in AJAX.
- XMLHttpRequest
- JSON
- DOM
- Fetch
In AJAX (Asynchronous JavaScript and XML), the XMLHttpRequest object is used to perform HTTP requests asynchronously. It allows you to send and receive data from a server without refreshing the entire web page.
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.