JavaScript | Object Properties
Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object. This collection may not follow any particular order. JavaScript provides the feature to add, delete and modify the properties. Properties are denoted by name:values pairs.
Syntax:
-
objectName.property
-
objectName["property"]
-
objectName[expression]
Properties:
- addition: It can add new objects by simply giving values to those new objects.
- deletion: It uses delete keyword, to delete a property from an object.
Example 1: Shows adding a new property to the existing object.
<!DOCTYPE html> < html > < head > < title > JavaScript Object Properties </ title > </ head > < body > < h1 >Geeks</ h1 > < h3 >JavaScript Object Properties</ h3 > < p id = "gfg" ></ p > <!-- Script to add object property --> < script > var employee = { name:"Steve", id:"123" }; employee.age="25"; document.getElementById("gfg").innerHTML = employee.name + " age " + employee.age + " years, and has unique id " + employee.id +"."; </ script > </ body > </ html > |
Output:
Example 2: Shows deleting a property from the existing object.
<!DOCTYPE html> < html > < head > < title > JavaScript Object Properties </ title > </ head > < body > < h1 >Geeks</ h1 > < h3 >JavaScript Object Properties</ h3 > < p id = "gfg" ></ p > <!-- Script to delete object content --> < script > var employee = { name:"Steve", id:"123" }; /* Delete employee id */ delete employee.id; document.getElementById("gfg").innerHTML = employee.name + " has unique id " + employee.id +"." ; </ script > </ body > </ html > |
Output:
There are different ways to accessing the object properties. Following examples demonstrate the different accessing techniques:
- Example: This example uses .property to access object element.
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
JavaScript Object Properties
</
title
>
</
head
>
<
body
>
<
h1
>Geeks</
h1
>
<
h3
>JavaScript Object Properties</
h3
>
<
p
>Using .property to access an object</
p
>
<
p
id
=
"gfg"
></
p
>
<
script
>
var employee = {
name:"Steve",
id:"123"
};
document.getElementById("gfg").innerHTML = employee.name
+ " has unique id " + employee.id ;
</
script
>
</
body
>
</
html
>
chevron_rightfilter_noneOutput:
- Example: This example uses [“property”] to access the object element.
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
JavaScript Object Properties
</
title
>
</
head
>
<
body
>
<
h1
>GeeksforGeeks</
h1
>
<
h3
>JavaScript Object Properties</
h3
>
<
p
>Using ["property"] to access an object</
p
>
<
p
id
=
"gfg"
></
p
>
<!-- Script to access an object -->
<
script
>
var employee = {
name:"Steve",
id:"123"
};
document.getElementById("gfg").innerHTML = employee["name"]
+ " has unique id " + employee["id"] ;
</
script
>
</
body
>
</
html
>
chevron_rightfilter_noneOutput:
- Example: This example uses for…in loop to access the object element.
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
JavaScript Object Properties
</
title
>
</
head
>
<
body
>
<
h1
>GeeksforGeeks</
h1
>
<
h3
>JavaScript Object Properties</
h3
>
<
p
>Using for...in loop</
p
>
<
p
id
=
"GFG"
></
p
>
<!-- Script to use loop to access object content -->
<
script
>
var gfg = "";
var employee = {
name:"Steve",
id:"123"
};
var z;
for (z in employee) {
gfg += employee[z] + " ";
}
document.getElementById("GFG").innerHTML = gfg;
</
script
>
</
body
>
</
html
>
chevron_rightfilter_noneOutput:
Recommended Posts:
- How to get a subset of a javascript object's properties?
- How to get font properties of particular element in JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- JavaScript | Window innerWidth and innerHeight Properties
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- Object.is( ) in JavaScript
- How to get a key in a JavaScript object by its value ?
- How to get the first key name of a JavaScript object ?
- Map vs Object in JavaScript
- Object.freeze( ) in JavaScript
- Object.assign( ) in JavaScript
- Object.keys( ) In JavaScript
- Object.isFrozen( ) In JavaScript
- How to iterate over a JavaScript object ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : riarawal99