jQuery.hasData(element) returns true if that DOM element already has jQuery internal data (since jQuery 1.5). Unlike jQuery.data(element), it never creates an empty cache — it only checks.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
var el = document.getElementById("box"); // or $("#box")[0]
jQuery.hasData(el) // false on a pristine element
$.data(el, "score", 10)
jQuery.hasData(el) // true
$.removeData(el, "score")
jQuery.hasData(el) // false
// Events also create data:
$(el).on("click", fn)
jQuery.hasData(el) // true
Call
What it does
hasData() Utility — Check Element Data Cache
Learn jQuery.hasData(): check if a DOM element has jQuery data without creating a cache, vs jQuery.data(), events count as data, examples, pitfalls, and FAQs.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Obtain a native element (document.getElementById or $el[0]).
Checking with hasData stays safe; calling jQuery.data(el) can create an empty cache.
jQuery
var el = document.getElementById("box");
console.log(jQuery.hasData(el)); // false — still pristine
jQuery.data(el); // returns {}, may create cache
console.log(jQuery.hasData(el)); // true — empty cache now exists
jQuery.hasData(element) returns true when that DOM element has any jQuery internal data — values from jQuery.data() / .data(), or bookkeeping from bound events. It returns false when no cache exists. Available since jQuery 1.5.
hasData only checks and never creates a cache. jQuery.data(element) always returns a data object and may create an empty one on first access. Use hasData for a side-effect-free existence check.
Yes. Binding with .on() (or older .bind() / .delegate()) stores handlers in the data API, so hasData becomes true until you .off() them or remove the element.
No. Pass a native DOM element — for example document.getElementById('box') or $('#box')[0]. .data() works on jQuery collections; hasData is the static low-level checker.
Use it before touching jQuery.data() when you only need to know whether a cache already exists — plugin init guards, cleanup audits, and avoiding empty cache creation on pristine elements.
Not directly. A data-* attribute alone does not create cache until .data() or jQuery.data() reads it. After caching (or after binding events), hasData returns true until the cache is cleared.