Skip to content
Related Articles

Related Articles

JavaScript TypeError – Cannot use ‘in’ operator to search for ‘X’ in ‘Y’
  • Last Updated : 19 Aug, 2020

This JavaScript exception Cannot use ‘in’ operator to search for ‘X’ in ‘Y’ occurs if in operator is used to search in strings, numbers, or other primitive types. It can not be used other than type-checking.

Message:

TypeError: Invalid operand to ‘in’ (Edge)
TypeError: right-hand side of ‘in’ must be an object, got ‘x’ (Firefox)
TypeError: cannot use ‘in’ operator to search for ‘x’ in ‘y’ (Firefox, Chrome)

Error Type:

TypeError

Cause of the Error: The in operator can be used only for to check if a property is in an object. This can not be used for search in strings, numbers, or other primitive types.



Example 1: in operator can not be used for string search, So the error has occurred.

HTML




<script>
"Geek" in "GeeksForGeeks"; // error here
</script>

Output:

TypeError: Invalid operand to 'in'

Example 2: in operator can not be used for string search, So the error has occurred.

HTML




<script>
var gfg = null;
"Geek" in gfg; // error here
</script>

Output:

TypeError: Invalid operand to 'in'
My Personal Notes arrow_drop_up
Recommended Articles
Page :