What is a pure function in JavaScript?
- A function that has no side effects
- A function that modifies global variables
- A function that always returns the same output for the same input
- A function that has asynchronous operations
A pure function is a function that always produces the same output for the same input and has no side effects. It does not modify external state, and it does not rely on external state. This property makes pure functions predictable and easy to test.
Can static methods be called on instances of the class?
- Yes
- No
- It depends on how the static method is defined
- Only if the class is instantiated with the new keyword
Yes, static methods can be called on instances of the class. However, it is more common and recommended to call static methods on the class itself, as they are associated with the class, not with a specific instance.
How can you iterate over the elements of a Set in JavaScript?
- Using for...of loop, forEach method, map method, filter method
- forEach method, map method, for loop, for...in loop
- for loop, for...of loop, forEach method, map method
- for...of loop, forEach method, filter method, for loop
To iterate over the elements of a Set in JavaScript, you can use the for...of loop, which provides a concise and readable syntax for traversing the Set's values. Alternatively, you can also use the forEach method to execute a provided function once for each Set element, ensuring a clean and efficient iteration process.
A WeakSet only stores _________, and its elements are garbage collected when there is no other reference to them.
- Objects
- Primitives
- Arrays
- Functions
A WeakSet only stores objects, and its elements are garbage collected when there is no other reference to them. WeakSets are useful for holding "weak" references to objects, meaning they won't prevent an object from being garbage collected.
In an AJAX call using Promises, _________ is a method used to parse the JSON response.
- .parseJSON()
- .toJSON()
- .parse()
- .json()
The correct method for parsing the JSON response in a Promises-based AJAX call is .json(). This method is specifically designed to extract and parse the JSON data from the response.
Question 2: In a large-scale application, how does the use of absolute imports in ES6 modules affect maintainability and refactoring compared to relative imports?
- Easier refactoring, better maintainability
- No significant impact
- Harder refactoring, lower maintainability
- Depends on the project structure
Using absolute imports can make refactoring more challenging, as changes to the file structure may require updating import paths throughout the codebase. This can result in lower maintainability compared to relative imports, which automatically adjust to the file's position. Consider the trade-offs based on project size and structure.
In ES6, setting "type": "______" in package.json informs Node.js to treat JavaScript files as ES modules.
- "ES6"
- "module"
- "script"
- "import"
By setting "type": "module" in package.json, Node.js is informed to treat JavaScript files as ES modules, allowing the use of import/export syntax and adhering to ES6 module behavior.
How does the syntax of an arrow function differ from a traditional function in JavaScript?
- Uses a shorter syntax (=>)
- Uses the function keyword
- Uses a different declaration keyword
- No difference in syntax
Arrow functions in ES6 use a shorter syntax (=>) compared to the traditional function declaration. This concise syntax is especially useful for simple one-liner functions.
To inherit properties and methods from another class in ES6, use the ________ keyword.
- Inherits
- Extends
- InheritsFrom
- ExtendsFrom
In ES6, the extends keyword is used to inherit properties and methods from another class.
What is the impact of dynamic imports on code splitting in a JavaScript application?
- Increases modularity
- Decreases modularity
- Has no impact
- Only works in Node.js
Dynamic imports in JavaScript have a positive impact on code splitting by allowing modules to be loaded on-demand. This enhances modularity, as only the necessary code is fetched when needed, reducing the initial load time of the application.