Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:18Length:1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Objects

Understanding “objects” in JavaScript starts with an understanding of “properties”. In this lesson, you’ll learn about both of these concepts.

2.4 Objects

Hello, and welcome to lesson number five where we learn about objects. Now, objects in JavaScript are, I think, pretty easy to understand, and it all starts from properties. A property is an association between a name and a value. For example, color equals red. That's a property. Properties can also have functions as values, in which case those properties are called methods, but we'll come back to that. Now objects are collections of properties, plain and simple. So let's have a look at how you can create an object, how you can access it, and all that good stuff. To create an object, you start, of course, with a variable. And let's call this variable, person. And we're gonna say, new object. That's one way to create it. Another way is to create, let's say, car equals curly brackets. So just like arrays, where we use square brackets to create them, or the syntax new array, objects we use new object and curly brackets, instead of square brackets. And what it can do now is start adding properties to these objects. So for example, person, what could we want to know about that person? We want to know its name, its age, maybe its height, right? So we can say person.name equals, let's say James. person.age equals, I don't know 43. person.height = 1.76. And actually this should be a number instead of a string. And we can also say person.married = true. So now if we type person in our terminal here, we get an object with name, age, height, married, set to the values you just saw. You can also access these properties in the following way. Person square brackets, and then inside double quotes, the name of the property, person name equals, let's change this to Kirk. Refresh, and now if we bring a person, it's gonna give me Kirk for its name. And that's how you can access these properties, and also how to change these properties. Both syntaxes are valid, but probably the dot followed by the name of the property is the easiest one to use. Now you can use objects inside objects, in the way that an object is the value for a property. So for example, if I add another property here called car, or let's not confuse it with this one here. Let's just delete this. Yeah, so I'm gonna say person.car equals a new object. And then I can go and say person.car.make = Honda for example, right? So now if I recall person, it gives me this object. And if I do person.car, it gives me another object. And if I do person car make, it gives me Honda. So this is how you can access these object properties, even if they're embedded in one another. Now you can also define these properties when you first initialize an object. So for example, let's delete this, and I can say var person equals, and I can specify it like this, name, the age, 30. Notice, the syntax here is a little bit different. So now when I access person, I get this object. As I was saying in the beginning, objects can also have methods, and a method is simply a property that has a function as its value. But we didn't learn about functions just yet, so there is no point in me talking about object methods right now. What I gave you right here are the basics of objects. How we can create them, how we can use them, how we can access their various properties. And with objects we've actually completed all seven data types in JavaScript. Now coming up in the next lesson, we'll learn about the available operators in JavaScript. See you there.

Back to the top
View on GitHub