Skip to main content

How to Resolve "TypeError: Cannot use 'in' operator" Error in JavaScript

The TypeError: Cannot use 'in' operator to search for '...' in ... is a common error in JavaScript that occurs when you try to use the in operator on a value that is not an object. The in operator is specifically designed to check for the existence of properties on an object, and it will fail if the value you are checking is a primitive type like a string, number, null, or undefined.

This guide will explain the correct use of the in operator and show you how to fix this error by using the appropriate methods for the data type you are working with, such as String.prototype.includes() for strings.

The Core Problem: The in Operator Requires an Object

The in operator's sole purpose is to check if a specified property (a key or an array index) exists in an object.

'propertyName' in myObject

where:

  • If myObject has a property named 'propertyName', it returns true.
  • If it does not, it returns false.

The error occurs when the value on the right-hand side of the in operator is not an object.

Correct Usage:

const user = {
name: 'Alice',
role: 'admin',
};

console.log('name' in user); // Output: true
console.log('age' in user); // Output: false

Cause 1 (Most Common): Using in to Search in a String

This is the most frequent cause of the error. A developer familiar with other languages might intuitively try to use in to check if a string contains a substring.

Example of problem:

const myString = 'Hello, World!';
const substring = 'World';

// PROBLEM: The `in` operator cannot be used on a string.
if (substring in myString) {
console.log('Substring found!');
}

Error Output:

Uncaught TypeError: Cannot use 'in' operator to search for 'World' in Hello, World!

Solution: to check if a string contains a substring, you must use the String.prototype.includes() method.

const myString = 'Hello, World!';
const substring = 'World';

// Correct: Use the `.includes()` method for strings.
if (myString.includes(substring)) {
console.log('Substring found!'); // Output: Substring found!
}

Cause 2: Using in on a null or undefined Value

The error will also occur if the variable you are trying to check is null or undefined, as these are not objects.

Example of problem:

let myObject = null; // This could come from a failed API call or a DOM query.

// PROBLEM: `myObject` is `null`, not an object.
if ('name' in myObject) {
console.log('The object has a name.');
}

Error Output:

Uncaught TypeError: Cannot use 'in' operator to search for 'name' in null

Solution: always add a guard clause to ensure your variable actually holds an object before using the in operator.

let myObject = null;

// Correct: First, check if `myObject` is a non-null object.
if (myObject && typeof myObject === 'object') {
if ('name' in myObject) {
console.log('The object has a name.');
}
} else {
console.log('The variable is not a valid object.');
}

Cause 3: Forgetting to Parse a JSON String

When you receive data from an API or read from localStorage, it is often in the form of a JSON string. Although it looks like an object, it is still a primitive string, and you cannot use the in operator on it.

Example of problem:

// PROBLEM: `jsonString` is a string, not an object.
const jsonString = '{"name":"Alice"}';

if ('name' in jsonString) {
console.log('The name property exists.');
}

Error Output:

Uncaught TypeError: Cannot use 'in' operator to search for 'name' in {"name":"Alice"}

Solution: you must first parse the JSON string into a native JavaScript object using JSON.parse().

const jsonString = '{"name":"Alice"}';

// Correct: Parse the string into an object first.
const myObject = JSON.parse(jsonString);

if ('name' in myObject) {
console.log('The name property exists.'); // Output: The name property exists.
}

Conclusion

The TypeError: Cannot use 'in' operator... is a clear signal that you are misusing the operator on a data type it was not designed for.

To solve it, follow this checklist:

  • If you are trying to search within a string, use the string.includes() method instead.
  • If you are trying to check for a property in an object, make sure the object variable is not null or undefined before using the in operator.
  • If you are working with data from an API, make sure you have parsed the JSON string into a JavaScript object with JSON.parse() before you try to inspect its properties.