Can an arrow function have its own this context?
- Yes, it has its own this context
- No, it shares the this context with the surrounding code
- Yes, but it inherits the this context from the parent scope
- No, it always refers to the global object
Yes, an arrow function has its own this context, which means it does not bind its own this but retains the this value of the enclosing lexical scope. This behavior can be advantageous in certain scenarios.
Can you use both named and default exports in the same ES6 module?
- Yes
- No
- Only if the module is in strict mode
- Only if the named exports are functions
Yes, you can use both named and default exports in the same ES6 module. This allows you to export a single "default" value along with multiple named exports from the same module.
The WeakSet does not have a _______ method which is available in a regular Set.
- size
- delete
- clear
- add
The WeakSet does not have a 'clear' method, which is available in a regular Set. The 'clear' method in a Set is used to remove all elements from the Set, but WeakSet lacks this functionality.
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.
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.
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.
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.
What role does the event loop play in asynchronous operations in JavaScript?
- It handles user interactions
- It executes code in a non-blocking way
- It manages DOM events
- It controls the flow of synchronous code
The event loop in JavaScript manages the execution of asynchronous tasks. It ensures that non-blocking code is processed efficiently, allowing the program to remain responsive. Understanding the event loop is crucial for writing efficient asynchronous JavaScript code.
Template literals are enclosed by ________ characters.
- Single backtick (`)
- Double quotes (")
- Single quotes (')
- Dollar sign ($)
Template literals in ES6 are enclosed by backticks (`). This allows for easy embedding of variables and expressions using ${}.
When using arrow functions with higher-order functions like Array.prototype.map, this will refer to the ________ where the arrow function was defined.
- Global Object
- Callback Function
- Scope Chain
- Enclosing Function
Arrow functions capture the this value from their surrounding lexical scope. In the context of higher-order functions, this is crucial to understanding and avoiding potential issues.
Pure functions should not modify any _________ state.
- Local
- External
- Internal
- Global
Pure functions should not have side effects, and modifying global state is considered a side effect. Therefore, they should not modify any global state.
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.