LearnFrenzy
Sign in Take a free test

DOM Manipulation · Predict the Output · Question 5

dataset values Answer, explanation & discussion

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

The page contains this HTML:

<div id="u" data-user-id="42" data-is-admin="false"></div>

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

main.js
const { userId, isAdmin } = document.getElementById("u").dataset;
console.log(userId + 1, typeof userId, isAdmin ? "admin" : "user");
  • A 43 number user
  • B 421 string admin Correct
  • C 421 string user
  • D 43 string admin
Correct Answer: Option B

dataset values are always strings: "42" + 1 is "421", and the non-empty string "false" is truthy.

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