# JavaScript: “TypeError: Cannot use 'in' operator to search for 'x' in y”

> TypeError: Cannot use 'in' operator to search for 'length' in a string, null or undefined. Parse the JSON string first, or use includes() for values.

Canonical: https://app369.com/fix/cannot-use-in-operator/
Last updated: 2026-11-08

Code throws TypeError: Cannot use 'in' operator to search for 'length' in '[{"id":1}]' (or for 'id' in undefined). The in operator checks whether an object has a property, and the value on its right was not an object. Most often it is a JSON string that was never parsed, or a value that is still undefined.

**Short answer**

Look at the value after in. If it is text that looks like JSON, parse it with JSON.parse (or fix the response so the library parses it). If it is undefined or null, the data has not arrived or the path is wrong, so guard before checking. If you meant to find a value in a string or array, use includes() instead; in only checks property names.

## **Why the value is not an object, and the fix**

1. 01 ### **A JSON string that was never parsed**
  > “The in operator can only be used to check if a property is in an object. You can't search in strings, or in numbers, or other primitive types.” [**MDN, TypeError: cannot use 'in' operator**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/in_operator_no_object) Data from localStorage, a message event, or a server that sends JSON with a text/plain content type arrives as a string. Libraries such as jQuery’s $.each then run in on it, which is where the classic search for 'length' message comes from. Parse it once, where it enters your code.```
const items = typeof data === 'string' ? JSON.parse(data) : data;
if ('length' in items) { /* ... */ }
```
2. 02 ### **undefined or null on the right** An API call has not finished, a property path is wrong, or a function returned nothing. Check for the value before using in, and fix where it should have been set.```
if (user && 'email' in user) {
  sendTo(user.email);
}
```
3. 03 ### **Searching for a value instead of a property**
  > “The in operator cannot be used to search for values in other collections.” [**MDN, in operator**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) in looks at property names, so 'b' in ['a', 'b'] is false (it checks for an index called b) and 'a' in 'abc' throws. Use includes() for values in arrays and strings, and has() for Sets and Maps.```
['a', 'b'].includes('b');   // true
'abc'.includes('a');        // true
new Set([1, 2]).has(2);     // true
```
4. 04 ### **Checking own properties only**
  > “If you want to check for only non-inherited properties, use Object.hasOwn() instead.” [**MDN, in operator**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) in also returns true for inherited properties such as toString. When you mean a property set on the object itself, use Object.hasOwn.```
Object.hasOwn(config, 'apiUrl');
```

Checked against the vendor’s docs on November 8, 2026: [MDN: TypeError: cannot use 'in' operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/in_operator_no_object); [MDN: in operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in).

## **Rather hand it over?**

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

- [from $450**App Fix Ticket**One reproducible bug in an existing mobile or web app, diagnosed and fixed.**See scope and start**](https://app369.com/fixed-price/flutter-firebase-bug-fix/)
- [from $450**AI-Built App Rescue**A Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.****No issues worth fixing, no charge.**** If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.**See scope and start**](https://app369.com/fixed-price/ai-app-rescue/)

Related: [**“Cannot read properties of undefined”**](https://app369.com/fix/cannot-read-properties-of-undefined/) · [**“Cannot convert undefined or null to object”**](https://app369.com/fix/cannot-convert-undefined-or-null-to-object/) · [**“Unexpected end of JSON input”**](https://app369.com/fix/unexpected-end-of-json-input/)

Questions

## **More on this error**

<details>

<summary>**Why does it say search for 'length'? **+</summary>

Code that loops over arrays (jQuery’s $.each and similar helpers) checks for a length property first. When it receives a string instead of the parsed array, that check throws.

</details>

<details>

<summary>**Is optional chaining a fix? **+</summary>

It avoids the crash for undefined and null, but not for strings. Make sure the value is the object you expect, then use in, Object.hasOwn or optional chaining as fits.

</details>

<details>

<summary>**Can you fix it for me? **+</summary>

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

</details>

A different error?

## **Describe it and get a written quote**

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.

[**Describe your problem **→](https://app369.com/contact/) [**All fixes**](https://app369.com/fix/)