Loading Please Wait...
Properties are the most important part of any JavaScript object.
Properties are the values associated with a JavaScript object.
A JavaScript object is a collection of unordered properties.
Properties can usually be changed, added, and deleted, but some are read only.
The syntax for accessing the property of an object is:
objectName.property // user.name
// or
objectName["property"] // user["name"]
// or
objectName[expression] // a = "name"; user[a]
//The expression must evaluate a property name
The JavaScript for...in statement loops through the properties of an object.
The block of code inside of the for...in loop will be executed once for each property.
for (let variable in object) {
// code to be executed
}
const user = { name:"John Doe", email:"john@gmail.com" };
for (let x in user) {
txt += user[x];
}
You can add new properties to an existing object by simply giving it a value.
user.mobile = "+91-0000000000";
// or
user["mobile"] = "+91-0000000000";
The delete keyword deletes a property from an object.
delete user.mobile;
// or
delete user["mobile"];
The delete keyword deletes both the value of the property and the property itself.
After deletion, the property cannot be used before it is added back again.
The delete operator is designed to be used on object properties. It has no effect on variables or functions.
The delete operator should not be used on predefined JavaScript object properties. It can crash your application.
Values in an object can be another object.
myObj = {
name:"John",
age:30,
cars: {
car1:"Ford",
car2:"BMW",
car3:"Fiat"
}
}
You can access nested objects using the dot notation or the bracket notation.
myObj.cars.car2;
// or
myObj.cars["car2"];
// or
myObj["cars"]["car2"];
Values in objects can be arrays, and values in arrays can be objects.
const myObj = {
name: "John",
age: 30,
cars: [
{name:"Ford", models:["Fiesta", "Focus", "Mustang"]},
{name:"BMW", models:["320", "X3", "X5"]},
{name:"Fiat", models:["500", "Panda"]}
]
}
To access arrays inside arrays, use a for-in loop for each array.
for (let i in myObj.cars) {
x += "<h1>" + myObj.cars[i].name + "</h1>";
for (let j in myObj.cars[i].models) {
x += myObj.cars[i].models[j];
}
}
All properties have a name. In addition they also have a value.
The value is one of the property's attributes.
Other attributes are: enumerable, configurable, and writable.
These attributes define how the property can be accessed (is it readable?, is it writable?)
In JavaScript, all attributes can be read, but only the value attribute can be changed (and only if the property is writable).
( ECMAScript 5 has methods for both getting and setting all property attributes)
JavaScript objects inherit the properties of their prototype.
The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved