This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
How to add keys/values to object with dot notation?
I am trying to use dot notation to add keys/values to my objects that I have created, but I cannot figure out how to do it. Here is my code that is not working:
var friends = {};
friends.steve = new Object();
friends.bill = new Object();
steve().firstName = 'Steve';
steve().lastName = 'Jobs';
steve().number = '555.555.1234';
bill().firstName = 'Bill';
bill().lastName = 'Gates';
bill().number = '555.444.1234';
I have tried it without the parenthesis after the object name, but that does not work either.
Answer 540af39c631fe97824005cb1
3 votes
Hi Deric, On lines two and three you have declared steve and bill as objects, but you also have declared them as properties (or keys) of object friends.
Because they are keys in friends you need to address them as such:
friends.steve.firstName = 'Steve';
friends.bill.firstName = 'Bill';
and so on…
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thank you for the explanation! I really appreciate it.