From the course: JavaScript Essential Training

Variables: Containers for everything - JavaScript Tutorial

From the course: JavaScript Essential Training

Start my 1-month free trial

Variables: Containers for everything

- Being able to name things is crucial to communication. If I have a bunch of objects, like all this stuff behind me and I want to draw your attention to one of them or a group of these objects, I need to use a name to refer to them. When we name objects we are effectively creating, named containers and then putting the objects inside those containers. So here's my son's pig hand puppet. To name it, I create a container with the name piggy and then I puts the hand puppet inside. Now, anytime I use the name, piggy, I'm referring to what's inside this container. And even though there are many hand puppets that look exactly the same only this particular one in the container is the one named, piggy. This is how we organize objects in the world, in our minds. Anytime we need to name an object or a group of objects to refer to them, we create a virtual container to hold those objects. Now what that container holds is entirely up to us. It could be a single object like the pig. It could be an array of objects, like these nuts. It could be a numerical value, the number five. It could be string of texts. It could even be a grouping of already defined and named objects like these different pens. This process of creating containers and placing objects inside them is also the way JavaScript handles named objects. Anytime we need to refer to some piece of data, we first create a container called a variable and then set its contents using the equals symbol. So the variable named, piggy, is set equal to the object which is the pig hand puppet. Anytime you encounter or use a single equal symbol in JavaScript, this is what's happening. A named variable container is created and something is placed inside that container. And when I say container is created I mean that pretty literally. When you define a variable in JavaScript, you effectively carve out space in the computer memory and place something inside that space. In this chapter, we'll take an in-depth look at variables to see how they work, why there are different types of variables, what we can put inside them, what we can do with them, and how we can compare, combine and even do math with them.

Contents