JavaScript in

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

The in operator returns true if a property name exists in an object or its prototype chain. For own properties only, prefer Object.hasOwn or hasOwnProperty.

JavaScript Syntax

JavaScript
const user = { name: "Ada" };
console.log("name" in user);
console.log("age" in user);
console.log("toString" in user);
Try it Editor

How It Works

  1. Own key. name exists.
  2. Missing key. age is false.
  3. Inherited. toString comes from Object.prototype.

Examples

Example 1 — Own only

Object.hasOwn ignores the prototype.

JavaScript
console.log(Object.hasOwn({ a: 1 }, "a"));
console.log(Object.hasOwn({ a: 1 }, "toString"));
Try it Editor

How It Works

  1. hasOwn skips inherited keys.

Example 2 — Array indexes

Checks index existence.

JavaScript
console.log(0 in ["x"]);
console.log(1 in ["x"]);
Try it Editor

How It Works

  1. Index 0 exists.
  2. Index 1 does not.

Example 3 — After delete

Key is gone.

JavaScript
const o = { a: 1 }; delete o.a; console.log("a" in o);
Try it Editor

How It Works

  1. delete removed a.

Frequently Asked Questions

in includes the prototype chain. hasOwn is own properties only.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful