Javascript Object Properties

As its name, this is simply declare name as string with any type of value assign to it. For example, the following object obj has a property whose name is the string "name" and it has value "Jimi". Similarly "age" is property name and "21" is value assigned to it.

Example

var Person = { name : "Jimi", age : 21 };

Adding New Property

In Javascript, you can add any number of property to the existing object. You need to give name of the new property and value assign to it. The below example creating new property "address" to the existing object "Person" with value as "12-13/A1". New "Person" object has three properties name, age and address.
var Person = { name : "Jimi", age : 21 };
Person.address = "12-13/A1";

Reading Property

In Javascript you can access or read object property via its name. If you see below example, it is accessing all three property of the "Person" object.
document.write(Person.name);
document.write(Person.address);
document.write(Person.age);

Removing Property

In Javascript delete keyword is use to delete the existing property from object.
var Person = { name : "Jimi", age : 21 };
delete Person.name;

Loop Through Property

You can loop through the object properties by using for..in loop to access all the properties.
var Person = { name : "Jimi", age : 21 };
//Adding new propert
Person.address = "23";
//Looping object properties
for (var key in Person) 
{
    if (Person.hasOwnProperty(key))
    {
		document.write(key + " = " + p[key] + "</br>");
    }
}

Complete Object Example

<html>
<head>
<script type="text/javascript">
var Person = { name : "Jimi", age : 21 };

//Reading object properties
document.write(Person.name);
document.write(Person.age);
document.write("</br>");

//Adding new property to object
Person.address = "12-13/A12";
document.write(Person.address);
document.write("</br>");

//Removing property from Person object
delete Person.age;
document.write(Person.age);
document.write("<br/>");

//Looping object properties
for (var key in Person) 
{
    if (Person.hasOwnProperty(key))
    {
		document.write(key + " = " + Person[key] + "</br>");
    }
}
</script>
</head>
<body>
</body>
</html>

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.