JavaScript Intermediate Practice
87 questions covering 17 JavaScript topics.
Quick Quiz
1. Which method adds an element to the end of an array?
2. Which method returns a new array?
3. Which method checks whether an element exists?
4. Which method adds an element to the beginning of an array?
5. Which method removes the last element?
6. Which method can add and remove elements at any index?
7. What does splice() return?
8. Why is unshift() generally slower than push() on large arrays?
9. What does indexOf() return when the value is not found in the array?
10. Which method returns a boolean instead of an index or element?
11. Which method is best suited for searching an array of objects by a specific property?
12. What does every() return when called on an empty array?
13. Which method can correctly detect NaN inside an array?
14. What does forEach() return?
15. Which method is guaranteed to return a new array with the same length as the original?
16. What happens if you call reduce() on an empty array without an initialValue?
17. Which method processes array elements from right to left?
18. Which method can shorten the resulting array compared to the original?
19. What is wrong with calling [40, 1, 5, 200].sort() without a comparator?
20. Which of these methods does NOT mutate the original array?
21. What argument would you pass to flat() to flatten an array of any nesting depth?
22. What does flatMap() do differently from map()?
23. What does join() return by default if no separator is provided?
24. Which method reliably checks whether a value is an array?
25. What does Array.from("hi") return?
26. What is the result of Array(7)?
27. What is the result of Array.of(7)?
28. What is the purpose of the second argument in Array.from(arrayLike, mapFn)?
29. What is a closure?
30. Why does a for loop using var with setTimeout() typically log the same final value multiple times?
31. How does using let instead of var fix the loop/setTimeout issue?
32. Which of these is NOT a typical practical use of closures?
33. In the createAccount() example, why does account.balance return undefined?
34. What does the following code log?
function makeCounter() {
let count = 0;
return function () {
count++;
return count;
};
}
const counter1 = makeCounter();
const counter2 = makeCounter();
console.log(counter1());
console.log(counter1());
console.log(counter2());35. What does this loop log?
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}36. What primarily determines the value of this inside a function?
37. How does an arrow function determine its this value?
38. What happens when you pass user.greet (a method) to setTimeout() without binding it?
39. Which method returns a new function with this permanently set, rather than invoking the function immediately?
40. Inside a class constructor, why might you write this.method = this.method.bind(this)?
41. What does this code log?
const obj = {
name: "Asha",
regular: function () {
return this.name;
},
arrow: () => {
return this.name;
},
};
console.log(obj.regular());
console.log(obj.arrow());42. What does this code log?
function Person(name) {
this.name = name;
setTimeout(function () {
console.log(this.name);
}, 0);
}
new Person("Rahul");