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?
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!