Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypeError: cannot use 'in' operator to search for 'x' in 'y'

The JavaScript exception "right-hand side of 'in' should be an object" occurs when thein operatorwas used to search in strings, or in numbers, or other primitive types. It can only beused to check if a property is in an object.

Message

TypeError: Cannot use 'in' operator to search for 'x' in 'y' (V8-based & Firefox)TypeError: right-hand side of 'in' should be an object, got null (Firefox)TypeError: "y" is not an Object. (evaluating '"x" in "y"') (Safari)

Error type

What went wrong?

Thein operator can only be usedto check if a property is in an object.You can't search in strings, or in numbers, or other primitive types.

Examples

Searching in strings

Unlike in other programming languages (e.g., Python), you can't search in strings usingthein operator.

js
"Hello" in "Hello World";// TypeError: cannot use 'in' operator to search for 'Hello' in 'Hello World'

Instead you will need to useString.prototype.includes(), for example.

js
"Hello World".includes("Hello");// true

The operand can't be null or undefined

Make sure the object you are inspecting isn't actuallynull orundefined.

js
const foo = null;"bar" in foo;// TypeError: cannot use 'in' operator to search for 'bar' in 'foo' (Chrome)// TypeError: right-hand side of 'in' should be an object, got null (Firefox)

Thein operator always expects an object.

js
const foo = { baz: "bar" };"bar" in foo; // false"PI" in Math; // true"pi" in Math; // false

Searching in arrays

Be careful when using thein operator to search inArrayobjects. Thein operator checks the index number, not the value at thatindex.

js
const trees = ["redwood", "bay", "cedar", "oak", "maple"];3 in trees; // true"oak" in trees; // false

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp