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 JavaScript Basics (Retired) Working With Numbers The Math Object

What is an Object in Javascript?

I didnt understand :(

2 Answers

Steven Parker
Steven Parker
229,670 Points

Don't worry, this concept can take a while to fully grasp. But simply, a computer "object" is a collection of code that represents something (perhaps a real-world physical object) that has the qualities of state and behavior. In the code, the state is represented by properties and attributes (variables), and behavior by methods (functions).

You'll see much better examples and more complete explanations in later courses. For now, you'll be using the Math object mostly as a collection of methods that do useful things with numbers. You can think of it as a "function library".

Ok this is how i understand it , so in real world say an object like car the state would be its color , size , model etc. which in this case would be variables and behavior would be accelerate , break , lights etc. which would be the functions. Am I right?

Steven Parker
Steven Parker
229,670 Points

Yes, that's a pretty good example! :car:

I might add that "lights" can be an example of both a state and a behavior, the state is whether they are on or off, and the behavior is turning them on or off.

An object can simply be thought of as a container. Though tangible objects are often used to illustrate the concept and this is fine, I think it can become tempting to begin thinking of objects in terms that are too concrete and tangible. An object is just a container that contains code that belongs together. There usually isn't a "right" or "wrong" way to group related code into containers, just better or worse ways to do it based on the program you are writing. But in any case, grouping related code together into objects allows developers to simply keep code more organized and more DRY (programming principle stating "don't repeat yourself". If it is not immediately clear to you how objects make your code more DRY, then don't worry--as you learn more, you will begin to understand why. At this point all you need to know is that objects group code. In the case of the math object, all sorts of mathematical and arithmetic functions and properties are grouped together in a convenient container for you. I hope this adds to your understanding :).

Thanks