In a Promise, if an error is not caught, it leads to a(n) ________.
- Rejection
- Resolution
- Exception
- Completion
In a Promise, when an error occurs and is not caught, the Promise is rejected. This can lead to unhandled promise rejections, causing issues in the application. Understanding how to handle promise rejections is crucial in asynchronous programming with Promises.
To handle errors in an async function, you should use a try...catch __________.
- Block
- Clause
- Mechanism
- Statement
To catch errors in an async function, you should use a try...catch statement. This helps in graceful error handling and prevents unhandled promise rejections.
Can async functions be used as constructors for new objects?
- Yes
- No
- Only in specific JavaScript engines
- Only with the use of decorators
No, async functions cannot be used as constructors for new objects. Async functions always return a Promise, and using them with the new keyword will result in a TypeError. Constructors should return objects, not Promises.
To iterate over an array's elements using a for...of loop, write for (const element _______ array).
- in
- of
- from
- within
In JavaScript, the correct syntax for iterating over an array using a for...of loop is for (const element of array). The of keyword is used to loop over the values in an iterable object like an array.
How do you access a static property within a class method?
- Using this.propertyName
- Using self.propertyName
- Using the class name followed by a dot and the property name
- Using super.propertyName
To access a static property within a class method, you use the class name followed by a dot and the property name. This is because static properties are associated with the class itself, not with instances, so you reference them through the class name.
To prevent modifications to an object’s prototype, the _________ method can be used to seal the prototype.
- Freeze
- Seal
- PreventExtensions
- Protect
The seal method in JavaScript can be used to prevent any further addition or deletion of properties on an object, including its prototype. This ensures that the prototype remains fixed and cannot be modified.
Can you use const within a for...of loop to declare a variable for each iteration?
- Yes
- No
- It depends
- Not applicable
In a for...of loop, the variable declared using const cannot be reassigned in each iteration. The loop variable retains its value throughout the loop, making it unsuitable for situations where reassignment is needed for each iteration.
To dynamically import a module based on a variable, the variable's value is included inside the __________.
- Template Literal
- Parentheses
- Square Brackets
- Curly Braces
To dynamically import a module based on a variable, the variable's value is included inside the square brackets of the import statement. This feature is known as dynamic import expressions, allowing developers to specify the module path at runtime. By using square brackets, the JavaScript interpreter treats the import statement as a dynamic expression, enabling more flexibility in module loading. Understanding this syntax is essential for dynamically loading modules based on runtime conditions, enhancing the adaptability of the application.
What happens if you try to import a named export that doesn't exist in the module?
- It will result in a runtime error.
- It will import a null value.
- It will import an undefined value.
- It will not have any effect, and the code will run without errors.
If you try to import a named export that doesn't exist in the module, it will result in a runtime error. The import statement expects the named export to exist, and an attempt to import a non-existent export will throw an error.
When building a web application that requests user data from an API and simultaneously posts data to another service, how can Promises enhance code readability and flow?
- Chaining Promises
- Callback Functions
- Asynchronous Callbacks
- Nested Callbacks
Promises allow for chaining, which enhances code readability. Chained promises provide a more elegant and sequential way of handling asynchronous operations compared to nested callbacks, making the code more readable and maintainable.
Given an API response with nested data structures, how would destructuring assignment simplify data extraction and handling?
- Extracting specific data becomes concise and readable
- Data extraction becomes slower
- Destructuring cannot be used with nested data structures
- Destructuring has no impact on data handling
Destructuring assignment in ES6 allows developers to extract specific values from nested data structures, making the code more concise and readable. By specifying the desired variables in the destructuring pattern, developers can directly access the required data, avoiding verbose syntax and improving code maintainability. This feature simplifies the process of working with complex API responses or nested objects, enhancing overall code efficiency and clarity.
In object destructuring, the rest operator can be used to gather the remaining properties into a new '__________'.
- Object
- Array
- Variable
- Function
In object destructuring, the rest operator (...) is used to collect the remaining properties of an object into a new array. This allows you to extract specific properties while capturing the rest in a convenient variable, often referred to as an array or an object.