How can you use a for...in loop to access the properties of an object?
- By using the index values.
- By using the Object.keys() method.
- By using the Object.entries() method.
- By using the Object.getOwnPropertyNames() method.
A for...in loop is used to iterate over the enumerable properties of an object. To access the properties of an object, you can use the Object.keys() method, which returns an array of the object's own enumerable property names. This allows you to loop through the keys (property names) of the object and access their corresponding values. It's a safer and more controlled way to work with object properties than a simple for...in loop.
How do you define a property inside a JavaScript object?
- Using dot notation
- Using square brackets
- Using a constructor function
- Using the prototype keyword
You can define a property inside a JavaScript object using dot notation, where you specify the object name followed by a dot and then the property name. For example: objectName.propertyName. This is the most common way to define object properties.
Using a switch statement with a very large number of cases might affect the _________.
- Code Readability
- Performance
- Variable Scope
- Error Handling
Using a switch statement with a very large number of cases might affect the performance of your JavaScript code. The larger the number of cases, the longer it may take to find a matching case, impacting the execution speed of your code. It's important to consider this when using switch statements in performance-critical code.
Which looping statement is suitable when the number of iterations is not known beforehand?
- for loop
- while loop
- do...while loop
- forEach loop
A while loop is suitable when the number of iterations is not known beforehand. It allows you to repeat a block of code as long as a specified condition is true. This makes it a flexible choice for situations where the loop's exit condition isn't predetermined.
What is the index of the last element of an array with 5 elements?
- 3
- 4
- 5
- -1 (JavaScript doesn't support accessing the last element directly)
The index of the last element in an array with 5 elements is 4. JavaScript arrays are zero-indexed, so the index of the last element is one less than the total number of elements in the array. The other options are not correct for this scenario.
The fetch function in JavaScript returns a Promise that resolves to the ________ of the request, whether it is successful or not.
- a) Response Object
- b) JSON Data
- c) Status Code
- d) URL
The fetch function in JavaScript returns a Promise that resolves to the Response object of the request, whether it's successful or not. This Response object contains information about the response, including headers and the response body, and allows you to handle the response appropriately.
You are troubleshooting an issue where the Fetch API call to a third-party API is not returning any data, and you suspect it might be due to a CORS policy. How might you validate and debug this issue?
- Use a CORS proxy
- Check the browser console
- Modify the server's CORS policy
- Reboot the server
To validate and debug a CORS (Cross-Origin Resource Sharing) issue, you can check the browser console for error messages, which often provide information about the CORS policy violation. Modifying the server's CORS policy or using a CORS proxy can help resolve such issues. Rebooting the server is unlikely to fix CORS problems.
What will be the output of console.log(typeof null); in JavaScript?
- "object"
- "null"
- "undefined"
- "number"
The expression console.log(typeof null); in JavaScript will output "object." This is a quirk in JavaScript because typeof null returns "object," even though null is not an object but a special value representing the absence of a value.
In which scenario might you prefer to use Object.create(null) over {} to create an empty object?
- When you need an empty object with no prototype chain (no inherited properties or methods)
- When you need an empty object with default prototype properties
- When you need an object with prototype properties
- When you need an object with getter and setter methods
Object.create(null) is used when you want an empty object with no prototype chain. This is useful when you want to create a clean slate object without inheriting any properties or methods from the Object prototype.
The insertBefore method is used to insert an element before the _________ child of a specified parent.
- first
- last
- next
- previous
The insertBefore method is used to insert an element before the specified next child of a parent element. It allows you to precisely control the position of the new element within the parent's children.