jQuery callbacks.has() Method — Check Membership

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

What Is has()?

callbacks.has([callback]) returns a boolean (since jQuery 1.7). With a function, it asks whether that handler is on the list. With no argument, it asks whether the list has any callbacks attached.

Remember
callbacks.has(foo)   // true if foo is on the list
callbacks.has(bar)   // false if bar was never added
callbacks.has()      // true if the list is non-empty
NeedUse
Register a handlercbs.add(fn)
Is fn on the list?cbs.has(fn)
Any handlers left?cbs.has()
Unregistercbs.remove(fn)

How to Use has()

Keep the function reference, then ask before you add or remove.

  1. Store handlers in named variables (not only inline anonymous functions).
  2. add() the ones you need.
  3. Call has(fn) for one handler, or has() for “any left?”.
  4. Use the boolean to guard add() / remove() or to assert in tests.
PieceRequired?Role
callbackNoFunction to look up; omit = “any attached?”
Return—true / false (boolean)

Pseudocode

Pseudocode
function has(callbacks, callback):
    if callback is provided:
        return callback is in callbacks.list   // reference match
    else:
        return callbacks.list has at least one handler

Cheat Sheet

Membership check — not fire status, not lock status.

TopicDetail
Syntaxcallbacks.has([callback])
SincejQuery 1.7
ReturnsBoolean
Match ruleSame function object (identity)
No-arg formtrue if list is non-empty
Cousinsadd / remove / empty

Live Preview

Add or remove named handlers and watch has() answers update.

Browser demo of callbacks.has() — same idea as the examples below.

Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Specific Function on the List

Official style: has(foo) vs has(bar).

jQuery
var foo = function (value1, value2) {
  console.log("Received: " + value1 + "," + value2);
};
var bar = function (value1, value2) {
  console.log("foobar");
};

var callbacks = $.Callbacks();
callbacks.add(foo);

console.log(callbacks.has(foo));  // true
console.log(callbacks.has(bar));  // false
Try It Yourself

How It Works

  1. Define two named functions, foo and bar.
  2. Add only foo to the list.
  3. has(foo) finds that same object → true.
  4. has(bar) finds nothing → false.

Example 2 — Any Callbacks Attached?

Omit the argument to ask if the list is non-empty.

jQuery
var cbs = $.Callbacks();
console.log(cbs.has());  // false

cbs.add(function () { /* ... */ });
console.log(cbs.has());  // true

cbs.empty();
console.log(cbs.has());  // false
Try It Yourself

How It Works

  1. Fresh list. has() is false.
  2. After add. At least one handler → true.
  3. After empty. List cleared → false again.
  4. Use case. Quick “is anyone listening?” check.

Example 3 — has() After remove()

Membership flips when you unregister the same reference.

jQuery
var onReady = function () {
  console.log("ready");
};

var cbs = $.Callbacks();
cbs.add(onReady);
console.log(cbs.has(onReady));  // true

cbs.remove(onReady);
console.log(cbs.has(onReady));  // false
Try It Yourself

How It Works

  1. Add onReady — it is on the list.
  2. has returns true.
  3. remove the same reference.
  4. has returns false — gone from the list.

Example 4 — Guard Duplicate add()

Add only when has(fn) is still false.

jQuery
var handler = function (msg) {
  console.log(msg);
};

var cbs = $.Callbacks();

function ensure(fn) {
  if (!cbs.has(fn)) {
    cbs.add(fn);
  }
}

ensure(handler);
ensure(handler);  // no second add

cbs.fire("once");
// once
Try It Yourself

How It Works

  1. ensure checks has(fn) first.
  2. First call adds the handler.
  3. Second call skips — already present.
  4. Alternative. $.Callbacks("unique") can also block duplicates.

Edge Cases & Pitfalls

Most surprises come from losing the original function reference.

identity

Anonymous twins do not match

add(function(){}) then has(function(){}) is false — different objects, same text.

fired

Not the same as fired()

has = who is registered. fired() = whether the list was invoked.

empty

empty() clears membership

After empty(), has(fn) and has() are both false.

unique

Prefer unique when you can

$.Callbacks("unique") blocks duplicate adds at creation time; use has when you need an explicit guard.

Browser Support

callbacks.has() has shipped since jQuery 1.7 with $.Callbacks. It works in all modern browsers with jQuery loaded.

✓ jQuery 1.7+

callbacks.has()

Return true if the list has any callbacks, or if a given function is registered.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
callbacks.has() Excellent

Bottom line: Call has(fn) for one handler, or has() to see if anyone is listening — both return a boolean.

Performance Notes

ApproachCostBest for
has(fn)O(n) scanLookup one handler
has()O(1) length checkNon-empty test
Repeated guardsCheap at small nPlugin subscribe paths

Callbacks lists are usually short. Prefer named handlers and optional unique over complex bookkeeping.

Key Takeaways

  • Role: boolean membership check for Callbacks.
  • Two forms: has(fn) or has() for any.
  • Identity: keep the same function reference.
  • Read-only: does not add, remove, or fire.

One line: has(fn) asks “is this handler registered?” — same object you add()ed.

Frequently Asked Questions

has() returns a boolean. With no argument, it tells you whether the list currently has any callbacks. With a function argument, it tells you whether that exact function is on the list.
has() checks membership (handlers on the list). fired() checks whether fire() or fireWith() has run at least once. A list can have handlers and never be fired, or be fired and later emptied.
Yes. Pass the same function object you added. Two separately written functions with identical code are not the same — has() will return false for the second one.
Yes. Officially, omitting the callback asks whether the list has any callbacks attached (non-empty). Use has(fn) when you care about one specific handler.
No. It is a read-only check. Use add(), remove(), empty(), or fire() to change or invoke the list.
Guard duplicate add() when you cannot use the unique flag, confirm a handler before remove(), or assert in tests that a subscriber was registered.

Next: fire()

Invoke every handler currently on the Callbacks list.

fire() tutorial →

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