Skip to content

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

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

    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

    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

    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 : MDN: TypeError: cannot use 'in' operator; MDN: in operator.

Rather hand it over?

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

Related: “Cannot read properties of undefined” · “Cannot convert undefined or null to object” · “Unexpected end of JSON input”

Questions

More on this error

Why does it say search for 'length'?

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.

Is optional chaining a fix?

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.

Can you fix it for me?

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.

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.