JavaScript Non-enumerable Property

Introduction

Most developers are familiar with object properties and how to access them, but many are unaware that some properties can be hidden from loops and property enumeration methods. These hidden properties are known as non-enumerable properties.

Non-enumerable properties play an important role in JavaScript because they allow developers to store internal data or utility methods without exposing them during object iteration. Many built-in JavaScript properties and methods are non-enumerable by default.

What is a JavaScript Non-enumerable Property?A

JavaScript non-enumerable property is used when you do not want to reassign the value of the key of the object. The non-enumerable property of the object does not show when you iterate through the object using for…in loop or using Object.keys() to get an array of property names.

In JavaScript, every property has an enumerable attribute.

The enumerable attribute can have one of two values:

  1. true → Property appears during enumeration.
  2. false → Property remains hidden during enumeration.

Why Are Non-enumerable Properties Useful?

Consider an object:


let user = {
    name: "John",
    age: 35
};

Suppose you want to store an internal ID that should not appear in loops.


{
    name: "John",
    age: 35,
    internalId: 101
}

Note: Displaying this internal property everywhere may not be desirable. Instead, you can make it non-enumerable.

Syntax

Non-enumerable properties are usually created using Object.defineProperty().


Object.defineProperty(
    object,
    propertyName,
    {
        value: propertyValue,
        enumerable: false
    }
);

Example:


let user = {};

Object.defineProperty(
    user,
    "id",
    {
        value: 101,
        enumerable: false
    }
);

The property exists but remains hidden during enumeration.

Creating a Non-enumerable Property

Example:


let person = {
    name: "John"
};

Object.defineProperty(
    person,
    "id",
    {
        value: 1001,
        enumerable: false
    }
);

console.log(person.id);

Output:

1001

Note: The property can still be accessed directly.

Use Non-enumerable Properties in for…in Loop


let person = {
    name: "John"
};

Object.defineProperty(
    person,
    "id",
    {
        value: 1001,
        enumerable: false
    }
);

for(let key in person) {
    console.log(key);
}

Output:

name

Note: The id property is skipped because it is non-enumerable.

Use Non-enumerable Properties and Object.keys()

Object.keys() returns only enumerable properties.

Example:


let user = {
    name: "John"
};

Object.defineProperty(
    user,
    "id",
    {
        value: 500,
        enumerable: false
    }
);

console.log(Object.keys(user));

Output:

[“name”]

Note: The hidden property does not appear.

Non-enumerable Properties and Object.values()

The non-enumerable property is excluded.


let user = {
    name: "John"
};

Object.defineProperty(
    user,
    "id",
    {
        value: 500,
        enumerable: false
    }
);

console.log(Object.values(user));

Output:

[“John”]

How to view Non-enumerable Properties?

Although hidden from loops, non-enumerable properties still exist.

You can use the method


Object.getOwnPropertyNames(object);

Example:


let user = {
    name: "John"
};

Object.defineProperty(
    user,
    "id",
    {
        value: 500,
        enumerable: false
    }
);

console.log(Object.getOwnPropertyNames(user));

Output:

[“name”, “id”]

Now both properties are visible.

Real-Life Example

Suppose you are building an online banking application.

Customer object:


let customer = {
    name: "John",
    balance: 5000
};

You need an internal account ID.


Object.defineProperty(
    customer,
    "accountId",
    {
        value: "ACC12345",
        enumerable: false
    }
);

Display customer information:


for(let key in customer) {
    console.log(key, customer[key]);
}

Output:

name John
balance 5000

Note: The account ID remains hidden.

Enumerable vs Non-enumerable Property

Feature Enumerable Non-enumerable
Accessible directly Yes Yes
Appears in for...in Yes No
Appears in Object.keys() Yes No
Appears in Object.values() Yes No
Appears in Object.entries() Yes No
Appears in Object.getOwnPropertyNames() Yes Yes

How to make Existing Properties Non-enumerable?

When you create a property Non-enumerable, then the property still exists but is hidden from enumeration.

Example:


let user = {
    name: "John"
};

Object.defineProperty(
    user,
    "name",
    {
        enumerable: false
    }
);

console.log(Object.keys(user));

Output:

[]

Common Mistakes

1. Assuming Non-enumerable Means Private

This is a wrong assumption.

A non-enumerable property can still be accessed directly. It is hidden from iteration, not from access.

2. Forgetting to Set enumerable: false

Wrong:


Object.defineProperty(
    user,
    "id",
    {
        value: 101
    }
);

If not specified, enumerable defaults to false in Object.defineProperty(), but developers should explicitly set it for clarity.

3. Using Object.keys() to Find All Properties

Wrong assumption:


Object.keys(user)

returns all properties.

Note: It only returns enumerable properties.

JavaScript Non-enumerable property – Interview Questions

Q 1: What is a non-enumerable property?
Ans: A property not listed during iteration.
Q 2: How to create it?
Ans: Object.defineProperty().
Q 3: Is length enumerable?
Ans: No.
Q 4: Can for...in access it?
Ans: No.
Q 5: Why use non-enumerable?
Ans: To hide internal properties.

JavaScript Non-enumerable property – Objective Questions (MCQs)

Q1. Non-enumerable properties are skipped by ______.






Q2. Which method defines non-enumerable properties?






Q3. Default value of enumerable is ______.






Q4. Which method lists only enumerable properties?






Q5. Non-enumerable properties can still be accessed using ______.






Conclusion

JavaScript Non-enumerable Properties are a powerful feature that allows developers to control how object properties behave during iteration. By setting the enumerable attribute to false, properties remain accessible but are hidden from loops and methods such as Object.keys(), Object.values(), and Object.entries().

This capability is widely used in JavaScript itself, where built-in methods and internal functionality are often stored as non-enumerable properties. Understanding how to create, inspect, and manage non-enumerable properties helps developers build cleaner APIs, hide implementation details, and gain a deeper understanding of JavaScript’s object system.

Related JavaScript Tutorials