LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 4

classList.toggle return values Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

The page contains this HTML:

<button id="b" class="btn">Menu</button>

What does the following JavaScript code print when it runs on this page?

main.js
const b = document.getElementById("b");
const first = b.classList.toggle("open");
const second = b.classList.toggle("open");
const forced = b.classList.toggle("open", true);
console.log(first, second, forced, b.className);
  • A false true true btn open
  • B true false false btn
  • C true true true btn open open
  • D true false true btn open Correct
Correct Answer: Option D

toggle returns whether the class is present afterwards; the second argument forces add (true) or remove (false).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links