Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Is it possible to create objects in objects and arrays in objects? :)

Hello, i am just curious if it is possible to first create an object and inside that object, create another object.

Same goes for arrays. Is it possible to first create an object and inside this object create an array :)

I am doing one of the "Extra Credit"-assignment in the course "Introduction to Programming". Here's what the assignment says:

"Try creating an object that represents you. Include all sorts of things about you, like your name as a string, and your favorite songs as an array. Explore how objects can be nested in other objects, and arrays."

Perhaps i have just misunderstood what they mean by "how objects can be nested in other objects, and arrays". Would be nice if someone could clear this out for me. :)

Thanks.

2 Answers

var foo = {

  bar: {
   'canI': true,
   baz : {
     buz: {
      'nestedObject': true
  },

  biz: [0,1,2]

}

Pretty much you can do whatever you want in JS :P

Thanks so much :D Are there any benefits and/or disadvantages to be able to do whatever you want in a programming language. Just curious to know, I am new to programming and wants to learn as much as possible. ;)

Once again, thanks.

With great freedom comes responsibility. With the above example you have to be much more aware of the dataTypes. JS does a good job of allowing you to combine and mismatch data types but if you call an operation that is specific to one dataType on an object that doesn't support it you won't know it until your program crashes at run time. With other language that are statically typed it would prevent you from doing this before even running the program.