Accessing data-* Attributes with `dataset`

The dataset property is a live map of an element's data-* attributes. It automatically converts HTML data-user-id to element.dataset.userId in JS, perfect for storing state. The footgun: name conversion is lossy; HTML attributes are always lowercased.
Why it exists
Before dataset, developers had to manually parse attribute names with getAttribute('data-user-id') and setAttribute. The dataset property provides a cleaner, object-like API for this common task, reducing boilerplate code and improving readability by creating a direct bridge between HTML attributes and JavaScript properties.
The mental model
Think of dataset as a live-updating JavaScript object that mirrors an element's data- attributes. It's not a static copy. When you change a property on element.dataset, the corresponding data- attribute in the HTML is immediately added, updated, or removed.
How it works
The browser handles the name conversion between HTML and JavaScript. In HTML, attributes are written in kebab-case (e.g., data-user-id). In JavaScript, you access this via dataset, where the data- prefix is dropped and the name is converted to camelCase (e.g., element.dataset.userId). Any uppercase letters in the original HTML attribute name are converted to lowercase before this transformation. For example, an element has a property div.dataset.itemId which returns the string "123". You can also write to it: div.dataset.lastUpdated = '2024-05-20' will add a data-last-updated="2024-05-20" attribute to the div.
When to use it
Use dataset to store simple, non-critical string data directly on an element. It's perfect for holding metadata that your JavaScript needs for presentation logic, like a record ID, a component's state ('open', 'closed'), or a URL for an API call, without needing a separate JS object to track it.
When not to use it
Avoid storing complex data (objects, arrays), sensitive information, or anything that should be part of your application's main state management system. Since all values are stored as strings, you must manually parse them into other types like numbers or booleans. Remember that this data is publicly visible in the DOM.
One canonical example
Given the HTML <button id="userBtn" data-user-id="42" data-action="fetch-details">View User</button>, you can access its data in JavaScript:
const btn = document.getElementById('userBtn');console.log(btn.dataset.userId); // "42"
console.log(btn.dataset.action); // "fetch-details"
// Now, let's add a new attribute from JavaScript:
btn.dataset.hasBeenClicked = 'true';// This adds data-has-been-clicked="true" to the button element in the DOM.
Interview question
Which statement accurately describes how `dataset` converts an HTML `data-*` attribute name like `data-User-ID` into a JavaScript property?
- a.The `data-` prefix is retained as `data`, and the rest of the name is converted to `camelCase`.
- b.The `data-` prefix is dropped, and the name is converted to `camelCase` directly, preserving original casing after the prefix.
- c.The `data-` prefix is dropped, and hyphens are replaced with underscores, converting the name to `snake_case`.
- d.The entire attribute name is first converted to lowercase, then the `data-` prefix is dropped, and the remaining name is converted to `camelCase`.Correct
Why? this is the answer
The card explicitly states that "Any uppercase letters in the original HTML attribute name are converted to lowercase before this transformation," and then "the data- prefix is dropped and the name is converted to camelCase." Option B is incorrect because it misses the crucial initial lowercasing of the HTML attribute name.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on javascript — each one lists the topics its interview covers.
See open roles