From the course: Learning the JavaScript Language

Objects

- [Instructor] So far, we've looked at some of the simplest types of data JavaScript knows about. In this video, we're going to look at objects which are a little more complex but completely foundational and super useful. A lot of what we're doing in the code we write could be viewed as teaching the machine to do things for us. JavaScript comes with a lot of simple data types built in. We've seen numbers, we've seen strings, we've seen Boolean . If we want to represent things that are more complex, that's where objects come in. Remember that we're assuming the scripts that we're writing know nothing unless we give them something to know. Figuring out what knowledge our programs need and in what form to represent that knowledge is a key decision. Objects are the main way in JavaScript and various other languages to encapsulate that knowledge in ways that are more complex than simpler data types allow on their own. The simplest way to make an object is with an object literal, similar to the string literals we saw before. Just type a curly brace and another one that closes it. That's an object. Now, to be able to inspect what we've done, we need to actually assign this to a variable. So let's say empty object equals an empty object. Now, if I look at the value of this empty object, I can see that JavaScript knows it's an object but it has nothing in it. Technically, it actually does have some properties but that's an implementation detail that we're not going to look at right now. For all intents and purposes, this is an object with nothing in it. To be really useful, of course, an object needs to have stuff. You can think of an object sort of like a box with labeled folders. Every useful object has a series of labels known as keys and values which are generally the data that we care about that are all grouped together in the object. Here's what this would look like with actual data. I'm going to assign another variable. We'll call this not empty object. And when I say it's like a filing system, it has labels. Now here in Node, I can just hit Return, and Node adds the three dots here, knowing that I'm continuing to write the same thing without executing it. The label that I'm assigning is a string. And when you're working in JavaScript you'll see these labels with quote marks or without. It's safest to use quote marks. So you don't accidentally use a JavaScript reserved word for a key, but because it's fewer keystrokes not to use them you'll see that a lot too. So anyway, we'll put in the name of the key which I'll use label in this case, then a colon, and then the value that that should contain, we'll just use the string value here. That value can be any type of data that JavaScript knows about. It could be numbers, strings, Boolean , even other objects, anything. Now that I've done that, I'll add my closing curly brace. I can hit Return and now I'm done. Let's check that value of not empty object. There it is. I have my label, and my value. To add more folders to your box, you can do the same thing. I'll just redo this, and now if I add a comma, I can add more labels. So here's my second one, and I will add the quotation marks this time. So the value of this one can be value two. And now having done so, I've redefined this variable And it has two properties and their values. And you can do this for as many items as you need in the object. The last value can have a comma on the end that used to cause issues in older versions of JavaScript, but it doesn't anymore. And so now I can type the variable name and get the whole thing printed out as I've done here. Objects are very simple to create, but you can make some very sophisticated data structures using nothing but these objects.

Contents