JavaScript Object Properties

Properties are the most imperative piece of any JavaScript object.

JavaScript Properties

Properties are the qualities related with a JavaScript object.

A JavaScript object is a gathering of unordered properties.

Properties can more often than not be changed, included, and erased, yet some are perused only.

Accessing JavaScript Properties

The punctuation for getting to the property of an article is:

objectName.property          /person.age


objectName["property"]       /person["age"]


objectName[expression]      /x = "age"; person[x]

Example 1

person.firstname + " will be " + person.age + " years old.";
Try it Yourself »

Example 2

person["firstname"] + " will be " + person["age"] + " years old.";
Try it Yourself »

JavaScript for...in Loop

The JavaScript for...in proclamation circles through the properties of an object.


for (variable in object) {
    code to be executed

The square of code within the for...in circle will be executed once for each property.

Looping through the properties of an object:


var individual = {fname:"John", lname:"Doe", age:25};

for (x face to face) {
    txt += person[x];
Try it yourself »

Adding New Properties

You can add new properties to a current article by just giving it an esteem.

Assume that the individual article as of now exists - you would then be able to give it new properties:


person.nationality = "English";
Try it Yourself »

Deleting Properties

The delete watchword erases a property from an object:


var individual = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
erase person.age;  /or erase person["age"];
Try it Yourself »

The erase watchword erases both the estimation of the property and the property itself.

After erasure, the property can't be utilized before it is included back again.

The erase administrator is intended to be utilized on article properties. It has no impact on factors or capacities.

The erase administrator ought not be utilized on predefined JavaScript object properties. It can crash your application.

Property Attributes

All properties have a name. Moreover they likewise have a value.

The esteem is one of the property's attributes.

Other qualities are: enumerable, configurable, and writable.

These qualities characterize how the property can be gotten to (is it decipherable?, is it writable?)

In JavaScript, all properties can be perused, yet just the esteem property can be changed (and just if the property is writable).

( ECMAScript 5 has techniques for both getting and setting all property attributes)

Prototype Properties

JavaScript objects acquire the properties of their model.

The erase catchphrase does not erase acquired properties, yet in the event that you erase a model property, it will influence all items acquired from the prototype.