What is a common pitfall when chaining multiple asynchronous operations using Promises?
- Not handling errors within each promise's chain.
- Mixing Promises with Callbacks.
- Using async/await inside a promise chain.
- Chaining promises without returning a new promise.
One common pitfall is forgetting to return a new promise when chaining promises. If a promise is not returned from each .then() callback, subsequent promises won't wait for the asynchronous operation to complete, potentially leading to unexpected behavior.
To pause and resume a generator function, the _________ keyword is used within the function body.
- yield
- await
- pause
- resume
In a generator function, the yield keyword is used to pause and resume the function's execution. It allows the generator to produce a sequence of values over time. The other options are not relevant to the purpose of pausing and resuming a generator.
Which of the following is a built-in higher-order function in JavaScript?
- map()
- add()
- concat()
- subtract()
The map() function in JavaScript is a built-in higher-order function that is commonly used to transform elements of an array. It takes a function as an argument and applies it to each element of the array.
In a scenario where a function calculates the total price of items in a cart, how would making this function pure affect its implementation?
- It would introduce side effects
- It would improve testability and predictability
- It would slow down the function execution
- It would require additional dependencies
Making the function pure in this scenario would involve removing external dependencies and ensuring that the function's output depends only on its input. This enhances testability and predictability, making the function easier to reason about and test in isolation. It also helps in avoiding side effects, which is a key characteristic of pure functions.
Can you reassign a new array or object to a variable declared with const?
- Yes, always
- Yes, but with restrictions
- No, it will result in an error
- Only if the variable is declared as a global constant
Variables declared with const cannot be reassigned to a new reference, whether it's an array, object, or any other data type. However, it's crucial to understand that this rule applies only to the reference itself, not the content of the variable. The content, if mutable (e.g., an object's properties), can still be modified.
Which method is used to send an AJAX request when using the Fetch API with Promises?
- fetch()
- send()
- request()
- ajax()
The fetch() method is used to send an AJAX request when utilizing the Fetch API with Promises. It is a modern and flexible API for making HTTP requests and handling responses.
Describe a scenario where using tagged template literals could be beneficial for sanitizing user input in a web application.
- They enable the creation of custom sanitization functions to process user input.
- They automatically escape special characters, preventing injection attacks.
- They provide a mechanism to enforce strict input validation rules.
- They allow for the easy integration of third-party sanitization libraries.
Tagged template literals allow developers to create custom sanitization functions, ensuring secure processing of user input and preventing potential injection attacks.
How are imports and exports handled differently in ES6 Modules compared to CommonJS?
- Explicit import and export statements
- Automatic global binding
- Using require and module.exports
- Implicit import and export statements
In ES6 Modules, imports and exports are explicit, and you use import and export statements to define dependencies. This is different from CommonJS, where dependencies are managed using require and module.exports.
How do you import a specific function from a module in ES6?
- import myFunction from 'module';
- import { myFunction } from 'module';
- import * as myFunction from 'module';
- import myFunction as 'module';
To import a specific function from a module in ES6, you use the syntax import { myFunction } from 'module';. This allows you to bring in only the functions or variables you need rather than the entire module.
How does recursion differ from iteration in JavaScript?
- Recursion is more memory-efficient
- Recursion uses a stack, while iteration uses a loop
- Recursion is slower than iteration
- Recursion is not supported in JavaScript
Recursion involves a function calling itself, creating a stack of function calls. In contrast, iteration uses loops for repetitive tasks. Understanding these differences is essential in choosing the right approach.