jQuery hasData() Utility

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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
CallWhat it does
hasData() Utility — Check Element Data CacheLearn 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

ParameterDescription
Step 1Obtain a native element (document.getElementById or $el[0]).
Step 2Call jQuery.hasData(element) (or $.hasData(element)).
Step 3Use the true / false result in an if or log.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Official Demo Sequence

Set data, remove it, bind a click, then unbind — print hasData each time.

jQuery
var $p = $("p"), p = $p[0];

console.log(jQuery.hasData(p));           // false
$.data(p, "testing", 123);
console.log(jQuery.hasData(p));           // true
$.removeData(p, "testing");
console.log(jQuery.hasData(p));           // false
$p.on("click", function () {});
console.log(jQuery.hasData(p));           // true
$p.off("click");
console.log(jQuery.hasData(p));           // false
Try It Yourself

How It Works

  1. Start clean. No cache → false.
  2. Store a value. $.data creates cache → true.
  3. Remove the key. Cache gone → false.

Example 2 — hasData vs jQuery.data()

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
Try It Yourself

How It Works

  1. Safe check. hasData reads only → still false.
  2. Touch with data. jQuery.data(el) with no key returns (and may create) an object.
  3. Afterward. hasData is now true even with no named values.

Example 3 — Bound Events Count as Data

Handlers live in the data API, so binding flips hasData to true.

jQuery
var $btn = $("#go"), el = $btn[0];

console.log(jQuery.hasData(el)); // false
$btn.on("click", function () { console.log("clicked"); });
console.log(jQuery.hasData(el)); // true
$btn.off("click");
console.log(jQuery.hasData(el)); // false
Try It Yourself

How It Works

  1. Before binding. No handlers → false.
  2. After .on. jQuery stores the handler → true.
  3. After .off. Handlers removed → false again.

Frequently Asked Questions

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.

Next: .data() Method

Learn how to store and read values on DOM elements with jQuery.

.data() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful