An async function always returns a __________.
- Promise
- Callback
- Synchronous Value
- Undefined
In an asynchronous function, the return value is always a Promise. This allows for better handling and chaining of asynchronous operations.
What is the default behavior of module resolution in ES6 when importing a module without specifying a file extension?
- Resolves to the module with the specified name
- Resolves to the module with the specified name, followed by ".js"
- Resolves to the module with the specified name, followed by ".mjs"
- Resolves to the module with the specified name, followed by ".json"
In ES6, when a module is imported without a file extension, the module resolution algorithm looks for the module with the specified name followed by ".js". If not found, it looks for the module with ".mjs" and then ".json". This flexibility allows developers to omit file extensions, and the resolution algorithm handles it accordingly.
Which ES6 feature can be particularly useful in writing more readable recursive functions?
- Arrow functions
- Destructuring assignment
- Rest and spread operators
- Default function parameters
Arrow functions have a concise syntax, making the code more readable. They are particularly useful in recursive functions where brevity and clarity are crucial.
If a class extends another class, the constructor of the child class must call _______ to access the parent's properties.
- super()
- parent()
- this()
- extend()
If a class extends another class, the constructor of the child class must call super() to access the parent's properties. The super() function is used to invoke the constructor of the parent class, ensuring that the parent's properties are initialized correctly.
What is the difference in execution timing between callbacks and Promises?
- Callbacks may lead to callback hell due to nested structures, affecting the execution sequence.
- Promises execute asynchronously, allowing better control over the flow of the program.
- Execution timing is the same for both callbacks and Promises.
- Callbacks always execute before Promises.
Promises provide a more straightforward approach to asynchronous programming, allowing developers to handle execution timing more efficiently. Callbacks, especially when nested, can lead to callback hell, making it challenging to manage the sequence of operations. This understanding is crucial for developers aiming to enhance code readability and manage asynchronous tasks effectively using Promises.
Can destructuring assignment be used with arrays, and if so, how does it differ from object destructuring?
- Yes, and it works the same as with objects
- No, destructuring can only be used with objects
- Yes, but the syntax is different from object destructuring
- Yes, and it uses square brackets for the pattern
Destructuring assignment can be used with arrays in JavaScript. However, the syntax differs from object destructuring. With arrays, square brackets are used to indicate the pattern, making it distinct from object destructuring that uses curly braces.
Can a child class in ES6 have a constructor without calling super()?
- Yes
- No
- Depends on the parent class constructor
- Only in certain scenarios
No, a child class in ES6 must call super() in its constructor to invoke the constructor of its parent class. This is essential for proper initialization and inheritance of properties from the parent class.
In JavaScript, what happens when the call stack is full, commonly known as 'Stack Overflow'?
- The program crashes, and an error is thrown
- The excess functions are moved to the heap
- The browser prompts the user with an error message
- The call stack automatically expands
When the call stack is full, a 'Stack Overflow' occurs, leading to a runtime error. This happens when the stack size exceeds its limit, usually due to excessive function calls, resulting in a crash.
In what way does the super keyword facilitate inheritance in ES6 classes?
- It allows access to the parent class's methods and properties
- It creates an instance of the parent class
- It prevents the child class from inheriting certain methods
- It is used to instantiate the parent class
The super keyword in ES6 classes is used to access the methods and properties of the parent class. It is commonly used in the constructor of the child class to call the constructor of the parent class and initialize the inherited properties.
What happens if you try to redeclare a variable declared with let in the same scope?
- It generates an error
- It assigns a new value to the variable
- It creates a new variable with the same name
- It deletes the variable
When you attempt to redeclare a variable with let in the same scope, it will generate an error. This is because variables declared with let cannot be redeclared in the same scope.
The "sideEffects" property in package.json helps tree shaking by indicating if a module may contain _______.
- Side Effects
- Pure Functions
- Impure Functions
- Async Functions
The correct option is (b) Pure Functions. The "sideEffects" property, when set to false, informs the module bundler that the module does not have side effects, making it eligible for tree shaking. Pure functions are side effect-free functions that only depend on their input parameters, making them suitable for elimination during tree shaking.
Can a module have both named and default exports?
- Yes, a module can have both named and default exports simultaneously.
- No, a module must choose either named or default exports.
- Modules can have multiple default exports, but only one named export.
- Named exports can only be used if there are no default exports in the module.
In ES6 modules, it's possible for a module to have both named and default exports. This flexibility allows developers to export a single main entity using default export and also export additional values using named exports.