JavaScript TypeError – Cannot use ‘in’ operator to search for ‘X’ in ‘Y’

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

TypeError: Invalid operand to 'in'

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

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

TypeError: Invalid operand to 'in'

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.