In a recursive function, how can default parameters be used to track state across recursive calls?
- Utilize default parameter values to maintain state
- Use default parameters only for base cases
- Default parameters are not suitable for recursive functions
- Recursive functions cannot use default parameters
In a recursive function, default parameters can be leveraged to maintain state across different recursive calls. This allows you to pass and update values without explicitly passing them in each recursive call. For example, consider a factorial function where the default parameter keeps track of the current product.
Can the super keyword be used in a class that does not extend another class?
- Yes
- No
- Only in static methods
- Only in arrow functions
No, the super keyword is used to call corresponding methods of the superclass in a subclass. If a class doesn't extend another class, there is no superclass, and using super is not valid.
Which method would you choose to execute a side effect for each array element without modifying the array?
- forEach
- map
- filter
- reduce
The forEach method is designed for executing a provided function once for each array element without modifying the array itself. It is commonly used when you need to perform side effects, such as logging or updating external variables, for each element.
In ES6, how can you define a method inside an object literal?
- Function shorthand notation
- Method shorthand notation
- Prototype method notation
- Arrow function notation
In ES6 object literals, you can define a method using the method shorthand notation. This provides a concise way to declare functions as properties of an object.
How does the super keyword differ when used in static methods compared to non-static methods?
- It behaves the same in both static and non-static methods
- It is not allowed in static methods
- It refers to the superclass in non-static and subclass in static methods
- It is used to invoke the constructor of the superclass in both cases
In a static method, 'super' refers to the parent class, while in a non-static method, it points to the parent class. This is because static methods are called on the class itself, and non-static methods are called on instances of the class.
Given a scenario where you need to flatten a deeply nested array, how would you implement this using recursion in ES6?
- Use Array.prototype.concat with recursion to flatten the array.
- Use a for...of loop to iterate through the array and flatten it.
- Implement a reduce function with recursion to flatten the array.
- Utilize the Array.prototype.flat method available in ES6.
In ES6, the reduce function can be leveraged along with recursion to flatten a deeply nested array effectively. The accumulator in reduce helps to accumulate the flattened result. This approach is versatile and applicable to various array structures. The other options either lack the necessary recursion or use methods not specifically designed for flattening deeply nested arrays.
In a scenario with a mix of synchronous and asynchronous code, how does the call stack and event loop manage the execution?
- The call stack executes synchronous code first.
- The event loop processes asynchronous code first.
- Both synchronous and asynchronous code are executed randomly.
- The call stack and event loop have no interaction.
The call stack prioritizes synchronous code, while the event loop manages the execution of asynchronous code by pushing it to the callback queue and executing it when the call stack is empty.
Can mixins in ES6 access private data of the classes they are mixed into?
- Yes, mixins have access to private data
- No, mixins cannot access private data
- It depends on the implementation of the mixin
- Private data is accessible only during class instantiation
Mixins in ES6 do not have direct access to the private data of the classes they are mixed into. Private data remains encapsulated within the class, ensuring data integrity and security.
How does method definition in ES6 classes affect the prototype chain?
- Adds methods to the subclass only
- Adds methods to the superclass only
- Does not affect the prototype chain
- Adds methods to both the subclass and superclass
In ES6 classes, method definitions impact the prototype chain. When a method is defined in a class, it gets added to the prototype of the class. When a subclass extends a superclass, its prototype chain includes both its own methods and those of the superclass. This ensures proper inheritance and method access.
What is the main advantage of using Promises over Callbacks in asynchronous JavaScript?
- Synchronous Execution
- Conciseness and Readability
- Callback hell elimination
- Procedural Approach
Promises in JavaScript provide a cleaner and more readable way to handle asynchronous code. With promises, you can avoid nested callbacks, making the code more maintainable. Option B is correct as it highlights the conciseness and readability advantages of using Promises over Callbacks.
The _________ dead zone refers to the time during which a let or const variable cannot be accessed.
- temporal
- execution
- initial
- declaration
The correct option is 'initial'. The temporal dead zone occurs between the entering of the scope and the actual declaration of the variable. During this time, trying to access the variable will result in a ReferenceError. It is called the 'initial' dead zone because it happens before the variable is initialized.
A class expression in ES6 can be named or unnamed, with the name being accessible inside the class's ________.
- Private Scope
- Protected Scope
- Public Scope
- Local Scope
The name of a class in ES6 is accessible only within the class's block scope, making it part of the class's internal structure.