Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is non-enumerable property in JavaScript and how can it be created?
Objects can have properties that don't show up when iterated through the particular object using Object.keys() or for...in loop. Those type of properties are called as non-enumerable properties.
What are Non-enumerable Properties?
Non-enumerable properties are object properties that are hidden from enumeration methods like Object.keys(), for...in loops, and Object.entries(). However, they can still be accessed directly using their property name.
Creating Non-enumerable Properties
To create a non-enumerable property we have to use Object.defineProperty() method. This is a special method to create non-enumerable properties in an object.
Syntax
Object.defineProperty(object, propertyName, {
value: propertyValue,
enumerable: false,
writable: true,
configurable: true
});
Example: Creating Non-enumerable Property
<html>
<body>
<script>
var person = {
name: 'gopal'
};
person.age = '21';
person['country'] = 'India';
Object.defineProperty(person, 'salary', {
value: '80,000$',
enumerable: false
});
document.write("Object.keys(): " + Object.keys(person) + "<br>");
document.write("Direct access to salary: " + person.salary);
</script>
</body>
</html>
Object.keys(): name,age,country Direct access to salary: 80,000$
Checking Property Enumerability
You can check if a property is enumerable using the propertyIsEnumerable() method:
<html>
<body>
<script>
var person = {
name: 'John'
};
Object.defineProperty(person, 'id', {
value: 123,
enumerable: false
});
document.write("name is enumerable: " + person.propertyIsEnumerable('name') + "<br>");
document.write("id is enumerable: " + person.propertyIsEnumerable('id'));
</script>
</body>
</html>
name is enumerable: true id is enumerable: false
Property Descriptor Options
Object.defineProperty() also lets you create read-only properties and control other behaviors:
| Option | Description | Default |
|---|---|---|
enumerable |
Shows in Object.keys() and for...in | false |
writable |
Can be modified | false |
configurable |
Can be deleted or redefined | false |
Example: Read-only Non-enumerable Property
<html>
<body>
<script>
var person = {
name: 'Alice'
};
Object.defineProperty(person, 'ssn', {
value: '123-45-6789',
enumerable: false,
writable: false
});
document.write("SSN: " + person.ssn + "<br>");
person.ssn = 'new-value'; // This won't work
document.write("SSN after modification attempt: " + person.ssn);
</script>
</body>
</html>
SSN: 123-45-6789 SSN after modification attempt: 123-45-6789
Common Use Cases
Non-enumerable properties are useful for:
- Internal metadata that shouldn't appear in JSON serialization
- Configuration properties that users shouldn't accidentally iterate over
- Methods and utilities that should be available but hidden from enumeration
Conclusion
Non-enumerable properties provide a way to add hidden properties to objects that won't appear in iterations but remain accessible. Use Object.defineProperty() with enumerable: false to create them for internal data and configuration.
