Advanced JavaScript Questions
11. What is the purpose of the Set object in JavaScript?
Correct Answer: B) To store unique values of any type, whether primitive values or object references.
Explanation: A Set is a collection of values where each value may occur only once. It's a perfect structure for ensuring uniqueness of elements and supports both primitive values and object references.
12. Which of the following is a true statement about JavaScript modules?
Correct Answer: B) JavaScript modules allow you to create private and public enclosures easily.
Explanation: JavaScript modules allow for easier management of dependencies and can contain both public and private code. Unlike scripts, modules are executed in their own scope, not the global scope, allowing for encapsulation.
13. What will the following code snippet output?
function* generatorFunction() {
yield 1;
yield 2;
yield 3;
}
const generator = generatorFunction();
console.log(generator.next().value);
console.log(generator.next().value);
console.log(generator.next().value);
console.log(generator.next().value);
Correct Answer: A) 1 2 3 undefined
Explanation: Generator functions allow you to define an iterative algorithm by writing a single function whose execution is not continuous. generator.next().value returns the next value yielded by the generator. After yielding all values, calling next() again will return undefined.
14. How do you ensure that a function does not get called more often than a specified period, even if it is invoked multiple times?
Correct Answer: B) Throttling
Explanation: Throttling is a technique used to ensure that a function is called at most once in a specified period. It's useful for controlling the rate at which a function is executed.
15. Which statement about async functions is NOT true?
Correct Answer: C) They can be used with the new operator to create instances.
Explanation: async functions are designed to handle asynchronous operations. They cannot be used with the new operator because they are not constructors and are meant to return promises, not instantiate objects.
16. What is the default behavior of the import statement in JavaScript if the specified module does not export a default?
Correct Answer: B) It will throw an error.
Recommended by LinkedIn
Explanation: If you try to import a default export from a module that doesn't export one, JavaScript will throw an error because the expected export is not found.
17. In the context of the this keyword in JavaScript, what will the following code output?
const obj = {
name: 'JavaScript',
getName: function() {
return this.name;
}
};
const getName = obj.getName;
console.log(getName());
Correct Answer: B) undefined
Explanation: When getName is called, it is no longer part of obj, so this does not refer to obj anymore. In a non-strict mode, this refers to the global object (window in browsers), where name is not defined, hence undefined. In strict mode, this would be undefined, leading to an error when trying to access name.
18. What is the output of this code?
let num = 0;
async function increment() {
num += await 2;
console.log(num);
}
increment();
num += 1;
console.log(num);
Correct Answer: A) 1 3
Explanation: The await expression causes increment to pause until a Promise is resolved. During this pause, the synchronous code continues to execute, incrementing num by 1, making it 1 before printing. After the await resolves, num is incremented by 2, leading to 3 when logged inside the async function.
19. What is the purpose of the Symbol type in JavaScript?
Correct Answer: A) To create completely unique identifiers.
Explanation: The Symbol type allows for the creation of unique identifiers. Symbols are immutable and can be used as object properties to avoid name collisions, among other uses.
20. What does the finally block in a try-catch-finally statement do?
Correct Answer: B) It is executed after the try and catch blocks, regardless of whether an exception was thrown or caught.
Explanation: The finally block is executed after the try and catch blocks complete, regardless of whether an exception was thrown or not. It is typically used for cleanup actions.