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.
Given a large project that requires conditional loading of modules based on runtime conditions, which module system would you prefer, ES6 Modules or CommonJS, and why?
- ES6 Modules
- CommonJS
- Both are equally suitable
- Depends on the project requirements
In a large project with conditional module loading, ES6 Modules offer advantages such as static analysis during the build process, enabling more efficient tree shaking and minimizing unused code. CommonJS, being synchronous, might introduce delays in loading modules, making it less suitable for dynamic scenarios.
What is the main difference between higher-order functions and callbacks?
- Higher-order functions take functions as arguments, while callbacks are functions passed as arguments.
- Callbacks are used only in asynchronous operations.
- Higher-order functions always return a value.
- Callbacks can only be anonymous functions.
Higher-order functions are functions that take other functions as arguments, offering flexibility, while callbacks are simply functions passed as arguments to other functions.
The _________ method executes a provided function once for each array element.
- forEach
- map
- filter
- reduce
The correct answer is forEach. This method iterates over each element in an array and executes a provided function once for each element. It is commonly used for tasks like logging, updating values, or performing side effects.
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.