This lesson, we're going to be looking at JavaScript objects and arrays, how objects work, what arrays
are, how to output a value of an object or an array, also using let and const within objects.
So objects give us the ability to contain more values within a single variable.
There's an example of an object on the right hand side.
So the values are written as naem value pairs.
So they include a property that you're going to be referencing in order to pull back that property value.
So each one of them will have a property and a value associated with it.
And that's how we get back and that's how we can reference the values that are contained within the
object.
So the property doesn't need quotes.
It's got the same naming rules as variables.
So in giving it a variable name, just keep that in mind that it works the same way as variables which
we are familiar with.
Objects can also contain functions which are referred to as methods when they are inside of an object.
So this one over here, we can call to person dot message, invoke the function and then we've got the
function which is going to return back.
Hello.
And using a value that's contained within the object as well, using person first and that will return
back that value.
There's also a quick code snippet on describing a car as an object.
So a notice.
We've got a color, we've got a year.
We've got to make a brand a price.
So this is a typical object structure where we've got related information all contained within one value.
When we look at car console, you can see all of the nice data is being referenced and return back there.
There's more information available about objects at the Mozilla Developer Network.
They have examples as well as some of the object structure.
So first, we're declaring the objects of this person and it can be empty.
This is how we declare that it is an object and then we can fill in the information about the object
later on.
You can also see more complex object where we've got an array, which we are looking at later on in
upcoming lessons.
We also have a number of value, string value, another array, and then we've got a couple functions
that are making use and they're all being contained within the one variable person.
So if you need to reference any of this information or any of these functions, you can access it with
the person, get the information that's contained within there so you can invoke the functions.
So it's the same way that we were typically invoking functions, except that we prefix it with the object
name, which is person in order to reference it and access that information.
There are two ways to get the values of DOT notation, which we've seen, and there's also bracket notation.
So there's more dynamic in the variables can hold a property name as well.
So this gives us more option on returning back the values and being able to utilize that information.
You can see that there is an example of the dot notation.
So that's how we reference the information in the DOT notation.
And then later on they also have how we can do it with the bracket notation.
So this is the same thing.
So this is dot notation, this is bracket notation and it's referencing the same information that's
contained within that object.
We also have another code snippet, code samples.
So first, let's set up a variable.
We can just give it a variable name of a equal to one and declaring an object.
So this object.
So this will be equal to test one.
So comma, separate them object value in here.
So test two and we can consider logout test, we can do test a one and then with bracket notation we
can do test and a one and notice that we do need to quote around there in order to return that value
back.
And what the bracket notation, we do have some more flexibility where we can add a value of a variable
so we can do test A plus the variable A and that's going to still return back test one because a plus
A is equivalent to a one.
You can also, if you increment the value of A and then if you do the same statement, you're going
to get test two, because now the value of A is two and we're looking to return back a plus A, which
is a two.
So now it comes to the lesson challenge.
So favorite things object.
So look around the room or think of whatever your favourite thing is.
It could be a computer, a car or house friend and describe them as a JavaScript object.
What are their properties and what are those values?
Add the values with dot notation or bracket notation and then output that information into the console.
Try it out and I'll show you a solution.
So go ahead, pause the video and I'll do a quick solution for you.
Coming up with a set up, an object.
We can call it my car and we already have a car down there.
And then we can use my car.
We can add in colour values to just equal it.
And you can console log out my car to see what's contained with.
In the myCar object, you can see right now, all we have is black in there and notice as well that
the object is CONSED because this is just referencing and there's going to be a little bit more on why
it's CONSED later on in the course of my car.
And we'll also try the bracket notation.
So adding in a value for a year and then we can equal that to whatever year we want to select.
And let's log out the value of car in the console.
So now that we see we've got color, we've got a year and we can continue to add in values as well as
we're declaring the object, we can set up some values at that point as well.