DevAcademy
PracticeJavaScriptIntermediate

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");

43. Which keyword is used to create an instance of a class?

44. What is the main purpose of the constructor method?

45. Where are instance methods defined inside a class actually stored?

46. What happens if you call a class without the new keyword?

47. How many constructor methods can a single class have?

48. What keyword is used to make one class inherit from another?

49. What must be called before this can be used in a subclass constructor?

50. How do you call a parent class’s version of an overridden method from inside the subclass method?

51. If class Dog extends Animal, what does Object.getPrototypeOf(Dog.prototype) equal?

52. If Dog extends Animal and rex is created with new Dog(), what does rex instanceof Animal return?

53. Which block always executes, regardless of whether an error was thrown or caught?

54. What must you call inside a custom Error subclass constructor to properly set up the message?

55. Which operator lets you check whether a caught error is an instance of a specific custom error class?

56. What does the stack property of an Error object contain?

57. Why can a synchronous try/catch not catch an error thrown inside a setTimeout callback?

58. How many default exports can a single module have?

59. How do you import a default export named User from "./user.js"?

60. What keyword lets you rename an imported binding?

61. What does dynamic import() return?

62. Which statement about ES modules is true?

63. Which of the following can be used as a Map key, but not as a plain object key without coercion?

64. Which method adds a value to a Set?

65. What does new Set([1, 2, 2, 3, 3, 3]) contain?

66. What is the correct way to check how many entries a Map has?

67. What is a concise way to remove duplicates from an array called numbers?

68. What does getMonth() return for a date in March?

69. What does Date.now() return?

70. Why might sameMomentA === sameMomentB be false for two Date objects representing the same time?

71. Which method correctly handles rolling over into the next month when adding days?

72. What is commonly recommended for complex date formatting needs?

73. What does the DOM represent?

74. Which method should you prefer when inserting untrusted, user-supplied text into an element?

75. What is returned by document.querySelectorAll(".item")?

76. Which classList method adds a class if it is missing and removes it if it is already present?

77. What must happen before an element created with document.createElement() appears on the page?

78. What is a key advantage of addEventListener() over setting element.onclick directly?

79. Which event object method stops the browser from performing its default action, such as a form submitting?

80. Which event fires on every keystroke inside a text input?

81. What is required in order for removeEventListener() to successfully remove a listener?

82. By default, during which phase does a listener added with addEventListener() run?

83. What does fetch() return?

84. Does fetch()'s promise reject when the server responds with a 500 status?

85. How do you send a JSON payload in a POST request with fetch()?

86. What does response.json() return?

87. How do you cancel a pending fetch() request?