Skip to content

JavaScript enumerable | Property

  • by

JavaScript enumerable property is one that can be included in and visited during for..in loops (or a similar iteration of properties, like Object.keys()).

If a property isn’t identified as enumerable, the loop will ignore that it’s within the object. Enumerable properties are properties whose internal enumerable flag is set to true.

JavaScript enumerable

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
    const obj = {
      Id: 1001,
      Name: 'John',
      Age: 27,
      Marks: 85
    };

    for (const key in obj) {
      console.log(key, obj[key]);
    }
  </script>

</body>
</html>

Output:

JavaScript enumerable Property

To check whether a property is enumerable or not, you can use the function propertyIsEnumerable(). It returns true if the property is enumerable or false.

<script>
    const obj = {
      Id: 1001,
      Name: 'John',
      Age: 27,
      Marks: 85
    };

    console.log(obj.propertyIsEnumerable('Id')); // true
</script>

What is the difference between iterable and enumerable in JS?

Answer: A bit about Iterable:

  • Iterable objects are a generalization of arrays. That’s a concept that allows us to make any object useable in a for..of the loop;
  • The iterable is an interface that specifies that an object can be accessible if it implements a method who is key is [symbol.iterator] link.

A bit about Enumerable:

  • It simply means that the property will show up if you iterate over the object using for..in loop or Object.keys;
  • An enumerable property in JavaScript means that a property can be viewed if it is iterated using the for…in loop or Object.keys() method. All the properties which are created by simple assignment or property initializer are enumerable by default.
  1. Enumerable [for in] looking at the properties that are inside of the object, not the values [only where enumerable: true – by default for all props];
  2. Iterable [for of] looking at the values;

Read more: https://stackoverflow.com/questions/68647965

Do comment if you have any doubts or suggestions on this Js property topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *