JavaScript tutorials Object Properties basic with examples

What is an Object in Javascript The object is a basic entity in javascript. It stores the list of key and value pairs.

Important points.

  • Every type of data extends Object class via prototype inheritance

  • object data contains properties with key and value pairs enclosed in square bracket []

  • The key contains data of type String and Symbol

  • value of type contains primitive types like String,number,bigint(latest),undefined,null and Function

  • It contains enumerable and non-enumerable properties

< Here is an example

const myobject = {
  id: "1",
  name: "john",
  department: "sales",
};
myobject.salary = 5000;

created myobject which id, name, and department with properties, these are called external properties. : Each object has internal inbuilt properties which are not visible like enumerable, value, etc.

Enumerable has the Boolean value true - these are shown when iterated with for loop and object.keys method, false - not visible.

Enumerable properties

enumerable properties that are iterable when for-in or object. keys method is called. These can be assigned in 2 ways. One is using property initialize on an object.

const myobject = {
  id: "1",
  name: "john",
  department: "sales",
};

The above example has id, name, and the department has key properties using assign expression Object properties are added using assignment with the property key

myobject.salary = 5000;

console.log(myobject.id);
console.log(myobject.name);
console.log(myobject.department);

for (let key in myobject) {
  console.log(key + "-" + myobject[key]);
}
or;
console.log(Object.keys(myobject));

Output is

id - 1;
name - johndepartment - sales[("id", "name", "department")];

All the properties created using initialize and assignment are iterable using for-in loop and object.keys.

non-enumerable properties

These properties are not visible when iterated the object. Create non-enumerable properties using defineProperty.

Object.defineProperty(myobject, "location", {
  value: "India",
  enumerable: false,
});
console.log(Object.keys(myobject));
["id", "name", "department"];

As you see in the above example, Created location property for myobject which contains enumerable:false indicates the non-enumerable property is not visible during Object.keys()iteration

Object.defineProperty(myobject, "location", {
  value: "India",
  enumerable: true,
});
console.log(Object.keys(myobject));
["id", "name", "department", "location"];

How to check property enumerable or not of an object

property IsEnumerable() method in object checks for property key is enumerable or not. if it is true, It is the object’s property.

const myobject = {
  id: "1",
  name: "john",
  department: "sales",
};

Object.defineProperty(myobject, "location", {
  value: "India",
  enumerable: false,
});
console.log(myobject.propertyIsEnumerable("id")); //true
console.log(myobject.propertyIsEnumerable("name")); // true
console.log(myobject.propertyIsEnumerable("location")); //false