The 'IN' Operator (for Enumerable Properties)
The in operator checks whether an object has a property with a given name. It is written as name in object, with the name as a string, a number or a symbol, and it returns true or false. The check counts properties the object inherits through its prototype chain as well as its own, and it counts every property, enumerable or not.
Checking for a property
Section titled “Checking for a property”const book = { title: 'Emma', author: 'Jane Austen' };
console.log('title' in book);console.log('price' in book);console.log('toString' in book);This prints:
truefalsetrue'toString' in book is true even though book defines no toString, because every plain object inherits it from Object.prototype.
in compared with checking the value
Section titled “in compared with checking the value”Testing the value, as in if (stock.pears), asks a different question from in. A property can exist and hold 0, '', false, null or undefined, and in still reports it, while a value test treats it as missing:
const stock = { apples: 12, pears: 0, plums: undefined, figs: null };
console.log('pears' in stock, Boolean(stock.pears));console.log('plums' in stock, stock.plums !== undefined);console.log('figs' in stock);console.log('grapes' in stock, stock.grapes !== undefined);
delete stock.apples;console.log('apples' in stock);This prints:
true falsetrue falsetruefalse falsefalseplums and grapes both read as undefined, but only in can tell that plums exists with an undefined value while grapes does not exist. After delete, the property is gone, so in returns false. Setting a property to undefined does not remove it.
Own and inherited properties
Section titled “Own and inherited properties”in looks along the prototype chain. To ask only about the object’s own properties, use Object.hasOwn, added in ES2022 as the safer replacement for obj.hasOwnProperty(name):
const defaults = { colour: 'blue' };const theme = Object.create(defaults);theme.size = 'large';
console.log('colour' in theme);console.log(Object.hasOwn(theme, 'colour'));console.log(Object.hasOwn(theme, 'size'));This prints:
truefalsetrueObject.hasOwn also works on objects with no prototype, such as those made by Object.create(null), which have no hasOwnProperty method to call.
Enumerable or not
Section titled “Enumerable or not”in ignores whether a property is enumerable. for...in and Object.keys list only enumerable properties, so a hidden property is found by in but left out of those listings:
const record = { id: 7 };Object.defineProperty(record, 'secret', { value: 'x', enumerable: false });
console.log('secret' in record);console.log(Object.keys(record));for (const key in record) { console.log(key);}This prints:
true[ 'id' ]idProperty Descriptors explains enumerable and the other property settings.
Arrays
Section titled “Arrays”For an array, in checks for an index, not a value. The indexes are the array’s property names, and length is a property too:
const colours = ['red', 'green', 'blue'];
console.log(0 in colours);console.log(3 in colours);console.log('length' in colours);console.log('green' in colours);console.log(colours.includes('green'));
const sparse = [1, , 3];console.log(1 in sparse, sparse[1]);This prints:
truefalsetruefalsetruefalse undefinedTo test whether an array holds a value, use includes. in is useful for spotting holes in a sparse array, where a missing index and an index holding undefined both read as undefined.
Primitives and type checks
Section titled “Primitives and type checks”The right-hand side must be an object. Using in on a string or number throws a TypeError, even though a string has a length:
try { console.log('length' in 'hello');} catch (err) { console.log(err.message);}console.log('length' in new String('hello'));This prints:
Cannot use 'in' operator to search for 'length' in hellotrueA common use is telling apart objects that have different shapes, where the presence of a property says which kind an object is:
function describe(shape) { if ('radius' in shape) { return `circle, radius ${shape.radius}`; } if ('width' in shape) { return `rectangle, ${shape.width} by ${shape.height}`; } return 'unknown shape';}
console.log(describe({ radius: 3 }));console.log(describe({ width: 4, height: 2 }));This prints:
circle, radius 3rectangle, 4 by 2The same operator, with a private name on the left, as in #balance in obj, checks whether an object has a private class field, as shown in Private Variables.
Steve Griffith covers this topic in his video JavaScript in Operator.