How does the call stack in JavaScript relate to the concept of 'blocking'?
- It represents the order of function calls
- It handles asynchronous tasks
- It causes delays in code execution
- It manages the memory allocation
The call stack in JavaScript tracks the execution of functions. When a function is called, it's added to the stack, and when it returns, it's removed. Blocking occurs when a function takes a long time to execute, causing subsequent code to wait. Understanding this helps optimize code for performance.
Is it possible to create a shared Symbol that is accessible across different parts of your code?
- No, Symbols are always local to the scope they are created
- Yes, Symbols can be shared globally
- Only if Symbols are declared as constants
- Symbols can only be shared within the same function
Symbols can be shared globally by using the Symbol.for() method, which creates a shared Symbol registry accessible across different parts of your code. This promotes consistency and reusability.
In ES6 Modules, imports and exports must be ________, unlike in CommonJS where they can be dynamic.
- Explicit
- Implicit
- Dynamic
- Conditional
In ES6 modules, imports and exports must be explicit, meaning they are statically determined at compile-time. This allows for better tooling support and optimizations. CommonJS, on the other hand, allows dynamic imports and exports.
Arrow functions are not suitable for methods that need their own this context, such as ________ functions.
- Event
- Callback
- Constructor
- Recursive
Arrow functions do not have their own this context, making them unsuitable for methods that need access to the object's this value. Constructor functions, which initialize objects and rely on this, should not be implemented using arrow functions.
How does destructuring work when the structure of the object does not match the destructuring pattern?
- It throws a syntax error
- It assigns undefined to the variables
- It ignores the mismatched values
- It automatically corrects the structure
Destructuring in JavaScript allows extracting values from objects or arrays, and when the structure doesn't match, it simply ignores the mismatched values, assigning undefined to the corresponding variables. This behavior is useful for extracting only the needed values and ignoring the rest.
The Array method __________ is an example of a higher-order function that takes a function as its argument.
- Map
- Filter
- Reduce
- Concat
The reduce method in JavaScript arrays is a higher-order function that takes a callback function to accumulate the elements of an array into a single result. It is commonly used for tasks like summing or product accumulation.
When implementing a module that provides extension hooks, how can Symbols be used to create non-conflicting method names?
- Using generic function names
- Using Symbols as method names
- Using random strings as method names
- Using module-specific prefixes
The correct option is using Symbols as method names. Symbols allow you to create non-conflicting method names by using them as keys for object properties. This is particularly useful in the context of module extension hooks, where different modules can use Symbols without risking naming conflicts. Generic function names, random strings, and module-specific prefixes may lead to conflicts, making Symbols a more suitable choice for this scenario.
Object literals in ES6 can include __________, a shorthand for defining functions.
- Arrow Functions
- Prototypes
- Generators
- Callbacks
ES6 introduced arrow functions as a shorthand for defining functions within object literals, offering a more concise syntax and lexically scoped 'this'.
What happens when you use the return keyword in a single-line arrow function?
- The function returns undefined
- The function returns the value after the arrow
- The function throws an error
- The function returns an array
In arrow functions, if the body is a single expression, it is implicitly returned. So, using the return keyword is optional. Option B is correct.
__________ can be used to execute a callback on every element in the array, creating a new array with the results.
- map
- forEach
- reduce
- filter
The forEach method can be used to execute a callback function on each element of an array. However, it doesn't create a new array with the results.