The block of code inside ______ will be executed if its condition is true and all preceding conditions are false.
- else-if
- switch
- try-catch
- default
In a JavaScript "switch" statement, the block of code inside "default" will be executed if none of the preceding conditions (cases) match. It serves as a fallback or default option when none of the cases match the given expression.
In JavaScript, the _______ method is used to create a new object using an existing object as the prototype of the newly created object.
- Object.create()
- Object.assign()
- Object.extend()
- Object.instantiate()
In JavaScript, the Object.create() method is used to create a new object with the specified prototype object. It allows you to create objects that inherit properties and methods from an existing object, making it a powerful tool for prototypal inheritance.
The switch statement evaluates expressions based on their _________.
- Values
- Cases
- Conditions
- Labels
The switch statement in JavaScript evaluates expressions based on their cases. Each case represents a possible value that the expression can take, and the code block associated with the matching case is executed. This allows for multiple execution paths based on different values of the expression.
When using a for...of loop with strings, each iteration will provide a single _______.
- Character
- Word
- Line
- Number
When using a for...of loop with strings, each iteration will provide a single character. This loop is useful for breaking down strings into individual characters for various processing tasks, such as counting characters or manipulating them individually.
Imagine you are implementing a feature to upload a file to the server. Which HTTP method would be most appropriate to use with the Fetch API for this purpose?
- GET
- POST
- PUT
- DELETE
To upload a file to the server, you would use the POST HTTP method with the Fetch API. The POST method is used for submitting data to be processed to a specified resource, which is suitable for file uploads. GET, PUT, and DELETE have different purposes.
How to declare a constant array in JavaScript?
- const arr = [];
- const arr = new Array();
- const arr = {}
- const arr = [1, 2, 3];
To declare a constant array in JavaScript, you can use the const keyword followed by square brackets, like const arr = [];. The const keyword ensures that the variable arr cannot be reassigned, making it a constant array.
You are debugging a web page and find that an element isn't being selected as expected with document.querySelector('.example'). What could be a possible reason if the class name is correct?
- Element not present in the DOM
- Element is inside an iframe
- JavaScript not loaded before the query
- Class name contains special characters or spaces
If an element with the correct class name isn't being selected with document.querySelector('.example'), a possible reason could be that the element is not present in the DOM at the time of the query. Ensure that the element you are trying to select exists and is rendered when your JavaScript runs. The other options are less likely causes.
What is the most common issue developers might face when working with closures and loops together?
- Variable hoisting
- Memory leaks
- Unexpected type coercion
- Event propagation
The most common issue when working with closures and loops together is the creation of memory leaks. This happens when closures inside loops capture references to variables that are continuously changing in the loop, preventing them from being garbage collected, and leading to increased memory consumption. It's crucial to understand and manage these cases to avoid performance problems.
To iterate over the keys in an object, you can use the for...______ loop.
- For...Of
- For...In
- For...Each
- For...While
To iterate over the keys in an object, you can use the for...In loop. This loop is specifically designed for iterating over object properties, allowing you to access each key. The for...Of loop, on the other hand, is used for iterating over the values of iterable objects like arrays.
While developing a web application, you create a class "Product" to hold the basic attributes of products in an e-commerce site. Later, you need to extend the functionality of some products which are on sale without altering the "Product" class. Which design pattern might be most appropriate to implement this additional functionality without modifying the existing "Product" class?
- Decorator Pattern
- Factory Method Pattern
- Singleton Pattern
- Observer Pattern
The Decorator Pattern is a suitable choice in this scenario. It allows you to add new behaviors or functionality (e.g., for products on sale) to existing classes (e.g., "Product") without modifying their structure. This ensures the open-closed principle and maintainability.