Skip to content

in

In JavaScript, the in keyword is an operator that returns true when an object has a property with a given key, either as its own property or through its prototype chain. The same keyword also separates the loop variable from the object in a for...in loop, which visits the object’s enumerable string keys.

The left operand is converted to a property key, and the right operand must be an object, so a primitive on the right throws a TypeError. Because the check follows the prototype chain, inherited names such as "toString" return true. Object.hasOwn() checks own properties only. A property set to undefined still exists, so in returns true for it.

Since ES2022, #name in object checks whether an object has a class’s private field, and it’s Baseline Widely available. The for...in loop also visits inherited enumerable keys and skips symbol keys. It’s covered under for, and looping over values is covered under of.

JavaScript in Keyword Syntax

The in keyword works as an operator and in the for...in loop:

Language: JavaScript Syntax
<key> in <object>
#<privateName> in <object>

for (<declaration> in <object>) <statement>

JavaScript in Keyword Examples

The in operator checks whether a property key exists on an object or its prototype chain:

Language: JavaScript
> const settings = { theme: "dark", fontSize: undefined };
undefined

> "theme" in settings
true
> "fontSize" in settings
true
> "toString" in settings
true

> "length" in "hello"
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in hello

The fontSize property exists even though its value is undefined, and toString comes from Object.prototype. A string primitive isn’t an object, so the last check throws.

In a for...in loop, the keyword introduces the object whose keys the loop visits:

Language: JavaScript
> const scores = { ada: 92, grace: 88 };
undefined

> for (const name in scores) {
…   console.log(`${name}: ${scores[name]}`);
… }
ada: 92
grace: 88
undefined

JavaScript in Keyword Use Cases

  • Checking whether an object has a property before reading it
  • Telling a missing property apart from one set to undefined
  • Checking for a class’s private field with #name in object
  • Listing an object’s enumerable keys with for...in

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026