Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

in

BaselineWidely available

Thein operator returnstrue if the specified property is in the specified object or its prototype chain.

Thein operator cannot be used to search for values in other collections. To test if a certain value exists in an array, useArray.prototype.includes(). For sets, useSet.prototype.has().

Try it

const car = { make: "Honda", model: "Accord", year: 1998 };console.log("make" in car);// Expected output: truedelete car.make;if ("make" in car === false) {  car.make = "Suzuki";}console.log(car.make);// Expected output: "Suzuki"

Syntax

js
prop in object#prop in object

Parameters

prop

A string or symbol representing a property name (non-symbols will becoerced to strings). Can also be aprivate element identifier.

object

Object to check if it (or its prototype chain) contains the property with specified name (prop).

Exceptions

TypeError

Thrown ifobject is not an object (i.e., a primitive).

Description

Thein operator tests if a string or symbol property is present in an object or its prototype chain. If you want to check for onlynon-inherited properties, useObject.hasOwn() instead.

A property may be present in an object but have valueundefined. Therefore,"x" in obj is not the same asobj.x !== undefined. To makein returnfalse after a property is added, use thedelete operator instead of setting that property's value toundefined.

You can also use thein operator to check whether a particularprivate class field or method has been defined in an object. The operator returnstrue if the property is defined, andfalse otherwise. This is known as abranded check, because it returnstrue if and only if the object was created with that class constructor, after which you can safely access other private elements as well.

This is a special syntax — the left-hand side of thein operator is a property identifier instead of an expression, but unquoted (because otherwise it's a string property, not a private element).

Because accessing private elements on objects unrelated to the current class throws aTypeError instead of returningundefined, this syntax allows you to shorten:

js
class C {  #x;  static isC(obj) {    try {      obj.#x;      return true;    } catch {      return false;    }  }}

To:

js
class C {  #x;  static isC(obj) {    return #x in obj;  }}

It also generally avoids the need for dealing with error handling just to access a private element that may be nonexistent.

However, thein operator still requires the private element to be declared beforehand in the enclosing class — otherwise, it would throw aSyntaxError ("Private field '#x' must be declared in an enclosing class"), the same one as when you try to access an undeclared private element.

js
class C {  foo() {    #x in this;  }}new C().foo(); // SyntaxError: Private field '#x' must be declared in an enclosing class

Examples

Basic usage

The following examples show some uses of thein operator.

js
// Arraysconst trees = ["redwood", "bay", "cedar", "oak", "maple"];0 in trees; // returns true3 in trees; // returns true6 in trees; // returns false"bay" in trees; // returns false (you must specify the index number, not the value at that index)"length" in trees; // returns true (length is an Array property)Symbol.iterator in trees; // returns true// Predefined objects"PI" in Math; // returns true// Custom objectsconst myCar = { make: "Honda", model: "Accord", year: 1998 };"make" in myCar; // returns true"model" in myCar; // returns true

You must specify an object on the right side of thein operator. For example, you can specify a string created with theString constructor, but you cannot specify a string literal.

js
const color1 = new String("green");"length" in color1; // returns trueconst color2 = "coral";// generates an error (color2 is not a String object)"length" in color2;

Using the in operator with deleted or undefined properties

If you delete a property with thedelete operator, thein operator returnsfalse for that property.

js
const myCar = { make: "Honda", model: "Accord", year: 1998 };delete myCar.make;"make" in myCar; // returns falseconst trees = ["redwood", "bay", "cedar", "oak", "maple"];delete trees[3];3 in trees; // returns false

If you set a property toundefined but do not delete it, thein operator returns true for that property.

js
const myCar = { make: "Honda", model: "Accord", year: 1998 };myCar.make = undefined;"make" in myCar; // returns true
js
const trees = ["redwood", "bay", "cedar", "oak", "maple"];trees[3] = undefined;3 in trees; // returns true

Thein operator will returnfalse forempty array slots, even if accessing it directly returnsundefined.

js
const empties = new Array(3);empties[2]; // returns undefined2 in empties; // returns false

To avoid this, make sure a new array is always filled with non-empty values or not write to indexes past the end of array.

js
const empties = new Array(3).fill(undefined);2 in empties; // returns true

Inherited properties

Thein operator returnstrue for properties in the prototype chain. This may be undesirable if you are using objects to store arbitrary key-value pairs.

js
const ages = { alice: 18, bob: 27 };function hasPerson(name) {  return name in ages;}hasPerson("hasOwnProperty"); // true

You can useObject.hasOwn() to check if the object has the key.

js
const ages = { alice: 18, bob: 27 };function hasPerson(name) {  return Object.hasOwn(ages, name);}hasPerson("hasOwnProperty"); // false

Alternatively, you should consider using anull prototype object or aMap for storingages, to avoid other bugs.

js
const ages = new Map([  ["alice", 18],  ["bob", 27],]);function hasPerson(name) {  return ages.has(name);}hasPerson("hasOwnProperty"); // false

Using the in operator to implement branded checks

The code fragment below demonstrates a static function that tells if an object was created with thePerson constructor and therefore can perform other methods safely.

js
class Person {  #age;  constructor(age) {    this.#age = age;  }  static isPerson(o) {    return #age in o;  }  ageDifference(other) {    return this.#age - other.#age;  }}const p1 = new Person(20);const p2 = new Person(30);console.log(p1.ageDifference(p2)); // -10console.log(Person.isPerson(p1)); // trueif (Person.isPerson(p1) && Person.isPerson(p2)) {  console.log(p1.ageDifference(p2)); // -10}

It helps to prevent the following case:

js
const p2 = {};p1.ageDifference(p2); // TypeError: Cannot read private member #age from an object whose class did not declare it

Without thein operator, you would have to use atry...catch block to check if the object has the private element.

You can also implement this as a[Symbol.hasInstance]() method of the class, so that you can use theinstanceof operator to perform the same check (which, by default, only checks for the existence ofPerson.prototype in the object's prototype chain).

js
class Person {  #age;  constructor(age) {    this.#age = age;  }  static [Symbol.hasInstance](o) {    // Testing `this` to prevent false-positives when    // calling `instanceof SubclassOfPerson`    return this === Person && #age in o;  }  ageDifference(other) {    return this.#age - other.#age;  }}const p1 = new Person(20);const p2 = new Person(30);if (p1 instanceof Person && p2 instanceof Person) {  console.log(p1.ageDifference(p2)); // -10}

For more examples, seePrivate elements and theclass guide.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-relational-operators

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp