JavaScript - Object Properties
JavaScript - Object Properties
JavaScript Object Properties
Anobject propertyin JavaScript is a key: value pair, where key is a string and value can be anything. The key in key: value pair is also called property name. So thepropertiesare association between key (or name) and value.
An object is in other terms a collection of properties (key: value pairs). However, key: value pairs are not stored in the specific order in the object. To write an object syntax, the curly braces are used. Each key: value pair is written within curly braces separated by a comma.
You can manipulate the object properties in JavaScript. For example, you can add, delete, or update the objectβs properties.
Syntax
You can follow the syntax below to define properties in the object.
const fruit = {
name: "Apple",
price: 100,
}In the above syntax, fruit is an object. The fruit object contains the name and price properties. The value of the name property is βApple, and the price is 100.
In an object, the key can either be a string or a symbol only. If you use another data type as a key, the object implicitly converts it into the string.
The property value can be anything like an object, set, array, string, set, function, etc.
Accessing Object Properties
There are 3 ways to access object properties in JavaScript.
- Using the dot notation
- Using the square bracket notation
- Using the expression
The Dot Notation
You can access the object property using the dot notation/ syntax.
obj.prop;In the above syntax, βobjβ is an object, and βpropβ is its property whose value you need to access.
Example
The βfruitβ object in the example below contains the name and price property. We access the object properties using the dot notation, and you can see property values in the output.
Output
The price of the Banana is 20The Square Bracket Notation
You can use the square bracket pair containing the property as a string followed by the object name to access a particular property.
obj["prop"]In the above syntax, we access the βpropβ property from the object.
You canβt access the property using the dot notation when you use invalid identifiers as an object key. So, you need to use the square bracket notation. The identifier is invalid if it starts from a number, contains a space, or a hyphen.
Example
In the example below, we access the fruit objectβs name and price property values.
Output
The price of the Banana is 20Using the expression inside the bracket
Sometimes, you require to access the object properties dynamically using the variable or expression. So, you can write the expression inside the square bracket notation. The expression can be a variable, a mathematical expression, etc.
obj[expression]The above syntax evaluates the expression first and accesses the property same as a resultant value from the object. You donβt need to write the expression in quotes.
Example
In the example below, the num object contains the number as a key in the string format and its word representation as a value.
We use the variable x to access the property value from the object. Also, we used the βx + 10β mathematical expression to access the object property dynamically.
Output
TwentyAccessing the Nested Object Properties
Accessing the nested object properties is very similar to accessing the object properties. You can either use the dot or square bracket notation.
Syntax
Obj.prop.nestedProp
// OR
Obj["prop"]["nestedProp"];In the above syntax, the prop is a property of the obj object, and nestedProp is a property of the βpropβ object.
Example
In the below code, the βcarsβ object contains the nested objects named OD and BMW. We access the nested object properties using the dot and square bracket notation.
Output
The model of Audi is Q7 and its price is 10000000
The model of BMW is S20D and its price is 8000000Adding or Updating the Object Properties
You can update or add new properties to the object using the dot or square bracket notation. You can access the object property and assign a new value to it. If the property already exists, it updates the property value. Otherwise, it adds the property to the object.
Syntax
Obj.prop = new_value;
OR
Obj["prop"] = new_value;In the above syntax, we update the value of the βpropβ property of the obj object.
Example
In the example below, we update the name and price property of the fruit object. Also, we add the expiry property to the fruit object.
Output
The price of Apple is 200 and it expires in 5 daysDeleting the Object Properties
You can use the βdeleteβ operator to delete the specific object properties.
Syntax
delete obj.prop;In the above syntax, obj.prop is an operator for the delete operator.
Example
In the example below, we delete the name property from the fruit object using the delete operator. The output shows that the fruit object contains only the price property after deleting the name property.
Output
The object after deleting the "name" property:
{"price":150}Enumerating the Object Properties
There are various approaches to enumerating the object properties. The Object.keys() method returns the objectβs keys in the array. However, we will use the forin loop to traverse through each property of the object.
Syntax
You can follow the syntax below to iterate through the object properties.
for (let key in table) {
// Use the key to access its value
}In the above syntax, βkeyβ is an object property, which can be used to access its value.
Example
In the example below, we have created the table object containing 3 properties. After that, we used the forin loop to traverse through each property of the object and access its value.
Output
Iterating the obejct properties
color: brown
shape: round
price: 10000Property Attributes
The object property contains four attributes.
- valueβ A value of the object.
- enumerableβ Contains boolean value representing whether the object is iterable.
- configurableβ Contains the boolean value representing whether the object is configurable.
- writableβ It also contains the boolean value, representing whether the object is writable.
By default, you canβt edit other attributes except the value attribute of the object property. You need to use the defineProperty() or defineProperties() methods to update other attributes.