4. adding properties



why is this code not working?

var friends=new Object();
friends.bill=new Object();
friends.steve=new Object();


Try using

in your code..


The Basics:
An object has one or more properties separated by a comma-,
Each property consists of a property-key and it's associated value

var friends ={ name: 'shantanu' , contact:'9557711991' };
The friends object has 2 properties:
1. a name property with property-key name and a string value 'shantanu'
2. a contact property with property-key contact
and a string value '9557711991'

You can either create an object using the literal notation as was done
defining the object friends.


You create an object using the constructor notation.
first you create an empty Object using the following syntax
either with
var friends = new Object();
var friends = {};
you add the properties by using the following syntax
friends.name = 'shantanu';
friends.contact = '9557711991';

var friends = {};
friends.name = 'shantanu';
friends.contact = '9557711991' ;
console.log( friends );

will give output:

{ name: 'shantanu', contact: '9557711991' }

You should also familiarize yourself with following syntax
In the constructor notation you can
add a property
the object-name dot literal property-key equal-sign associated value
friends.email = "alex@codecademy" ;
the object-name squarebracket-open the property-key as string literal squarebracket-close equal-sign associated value
friends["email"] = "alex@codecademy" ;
using variables

var thePropertyKey = "email";
var theAssociatedValue = "alex@codecademy.com";
friends[thePropertyKey] = theAssociatedValue;
console.log( friends );

will give the output:

{ name: 'shantanu', contact: '9557711991', email: 'alex@codecademy.com' }


thank you that helped alot


what is wrong with my code. Its not passing

var friends ={};
friends.name ="Steve";
friends.address = "washington blvd";
friends.bill ={};
friends.steve ={};
friends.sanjana ={};
friends.bill.firstName = "Bill";
friends.bill.lastName ="Gates";
friends.bill.number = 789654;
friends.bill.address = ["essex street","76", "jersey city","NJ"];
friends.steve.firstName ="Steve";
friends.steve.lastName = "Jobs";
friends.steve.number =987654;
friends.steve.address = ["washington blvd", "4401", "jersey city","NJ"];
friends.sanjana.firstName ="Sanjana";
friends.sanjana.lastName = "Dhanwantri";
friends.sanjana.number =989765;
friends.sanjana.address =["10","regent street"];


What is this for

 friends.name ="Steve";
 friends.address = "washington blvd";

and it might be that the number should be a String-Value


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.