JavaScript tutorials Object Properties basic guide with examples


Object introduction
Object is a basic entity in java script. It stores the list of key and value pairs.

Important points.

  • Every type of data extends Object class via prototype inhertence

  • object data contains properties with key and value pais 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 examples

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 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 object.

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

The above example has id,name and department has key properties using assign expression Object properties are added using assignment with 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 iteratble 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 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

propertyIsEnumerable() method in object checks for property key is enumerable or not. if it is true, It is object own 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  

Similar Posts