Basics

includes vs indexOf — and why your check lets the guest in but not the admin

Illustration: a yes/no lamp and a pointer at the very first box

Look at this bug that feels like magic. There's an "allow into the admin panel" check:

const roles = ["admin", "user"];
if (roles.indexOf(role)) {
  openAdminPanel();
}

The admin shows up — and gets turned away. A guest who isn't on the list at all — gets in. The code works exactly backwards. And it's not the logic's fault, it's the choice of method.

What each one does

Both methods look for a value in an array or a string. The difference is what they answer.

  • includes answers "is it there or not." It returns true or false.
  • indexOf answers "where." It returns the position number. And if it finds nothing — -1.

Now the bug makes sense. "admin" sits at position 0 — first in the list. And 0 counts as false in a condition. The guest isn't on the list — indexOf returned -1. And any non-zero number in a condition is true. More on these "almost trues" in the booleans explainer.

We checked in Node 22: !!["admin","user"].indexOf("admin") gives false, and for "guest" — true. It all adds up.

Point-by-point comparison

  • What it returns. includes: true or false. indexOf: a position or -1.
  • How it reads in an if. includes goes into a condition as is. indexOf must be compared: indexOf(x) !== -1. Forget the comparison — get the bug above.
  • Finding NaN. [NaN].includes(NaN) is true. [NaN].indexOf(NaN) is -1 — not found. Rare, but NaN does show up in spreadsheet data.
  • Holes in an array. The array [, 1] has an empty slot. includes(undefined) finds it, indexOf(undefined) doesn't.
  • Searching a string. Both work on strings too: "hello".includes("ll") is true, "hello".indexOf("ll") is 2. Both find an empty string at position zero.
  • Age. indexOf has been around forever. includes arrived for arrays in the 2016 standard and works in every modern browser.
  • Finding an object by field. Neither can. Both compare the whole value. For "find the user with id 7" you need find or some.

Which one to pick

The verdict, no diplomacy:

  • Need "is it there" — use includes. That's 9 cases out of 10. The code reads like a sentence, and the zero bug is impossible.
  • Need the position — use indexOf. For example, to cut a string up to the found spot or remove an item by its number. But always compare with -1.
  • See if (arr.indexOf(x)) without a comparison — it's a bug. Even if it works for now. Ask the AI to switch it to includes.
Weak promptWhy doesn't the role check work?
Strong promptThe check if (roles.indexOf(role)) lets guests into the admin panel and blocks admin. Explain why this happens because of what indexOf returns, and rewrite the check using includes. Find every place in the project where an indexOf result is used in a condition without comparing to -1, and list them.

The last line of that prompt is the most useful. This bug rarely lives in one place: the AI copies its own pattern across the whole project.

Searching for an object — some and find

A common mix-up: the AI writes users.includes({ id: 7 }) and is surprised it's always false. Two objects are equal only if they're the same object in memory. A new { id: 7 } is a different one, even with the same fields.

For searching by field there's a set of similar methods:

  • some — "is there at least one like this." The includes of objects: users.some(u => u.id === 7) returns true or false.
  • find — "give it to me." Returns the object itself or undefined.
  • findIndex — the indexOf of objects: a number or -1. And the same zero trap in a condition.

The choice works the same way: need a yes/no — some; need the record itself — find.

Is includes faster than indexOf?

Practically the same: both scan the array in order. If you run thousands of checks on a big list, a Set with its has method is faster — it finds a value without scanning.

Can I search from a certain position?

Yes, both take a second argument — where to start. arr.includes(x, 3) searches starting from the fourth item.

How do I check whether an object has a key?

includes won't help here — it's for values. For keys there's Object.hasOwn(obj, "email"). The "email" in obj operator also works, but it finds inherited keys too: "toString" in {} is true.

What does this look like in Python?

There, "is it there" is the in operator: "admin" in roles. The position comes from roles.index("admin") — but if the value is missing, Python doesn't return -1, it raises an error. So the "backwards" check from JavaScript can't happen there.

What about letter case?

Both are case-sensitive: "Admin" and "admin" are different strings. Normalize before comparing, for example with toLowerCase().

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →