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
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) { /* ... */ }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); }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); // true04
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.
- from $450App Fix TicketOne reproducible bug in an existing mobile or web app, diagnosed and fixed.See scope and start
- from $450AI-Built App RescueA 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
Related: “Cannot read properties of undefined” · “Cannot convert undefined or null to object” · “Unexpected end of JSON input”