- [Narrator] In the real world,
if you want someone to hand you something
you mentioned it by name.
So you say, hey, can you hand me that backpack, please?
The same thing goes for JavaScript.
Anytime you want to access an object
or anything else contained inside a variable
you simply call it by its name
and JavaScript will hand it to you.
In the exercise files for this movie
there is an object with the name backpack.
If you go over to the console
you can call that object by simply naming it directly.
So if I type in backpack here and hit return
I get the entire object
and I can then dig deeper into it to see what's going on.
So here I can see all the properties and the methods
and I can even open the methods to see what they contain.
It works the same way in JavaScript.
So let's say you want to output this object in its entirety
in the console for reference and do so automatically
so you don't have to go
into the console and call it by name.
JavaScript has a series of console methods for this purpose.
And the most common one is called console log.
Let me make some space here.
And then we simply say, console log backpack.
Save this.
Go back in the browser.
And now you see the backpack object outputs
in the console automatically.
If I reload the page
you'll see it keeps popping in and we get the same thing.
Here's the entire backpack object
and we can get access to it automatically.
So this code down here tells the browser
grab the object and log it in the console.
Now that you see how it works
we can provide some additional context here
by adding a string of text, explaining what it is, because
if we look at this output right now, it's not very helpful.
We can see there's an object
we don't know which object it is and everything like that.
So I can go into the console log here
and add a string of text to the front,
to say
the
backpack
object
colon
then end my quotation mark, and add a comma.
This format tells the console
output first this string of text
then add a space and then add the object in question.
So we'll save that, go back in the browser.
And now we can see it says the backpack object
and then we have the object itself.
So this is a really simple way
of outputting context into the console
so we can see what's going on and then providing context.
So calling an object by its name in JavaScript
is effectively the same thing
as picking up an object in the real world.
You now have the entire object
and all its properties and functions.