Chapter 2 - Variables

The first comment in our application (the text within the /* ... */) was this:

  1. Declare variables containing the URL for the Open Weather Map API and your API Key.
var apiUrl = 'http://api.openweathermap.org/data/2.5/weather';

var apiKey = 'fa32f'; // REPLACE WITH YOUR KEY!

Let's look at the first variable declaration on the first line. The part on the right surrounded by single quotes (') is a string we don't need the computer to do much with. A string is a sequence of characters (could be letters, numerals, or any other symbols) surrounded by quotes. The example above is an instruction for the computer to create a string and store it in its memory. In this scenario our string is part of the URL where we're going to get our weather data from.

Note that Strings in JS can be enclosed by single (') or double (") quotes. So 'this is a valid string' and "This is a valid one too". But they must be enclosed by quotes or else it's not a string!

var apiUrl = 'http://api.openweathermap.org/data/2.5/weather';

We declare a variable with the keyword var, and then assign a value to it with the following equals sign (=). This just means we instruct the computer to (1) allocate some memory for our application and (2) store in it a reference to where our string will live in the computer's memory. By keeping a reference to our string, we can now access it using the apiUrl variable.

If you were to not trust your computer (you're just getting to know each other, that's cool), simply run the following commands in your Console and see for yourself. Again, the first command stores reference to our URL string in a variable called apiUrl. The second command simply prints its content to the screen.

var apiUrl = 'http://api.openweathermap.org/data/2.5/weather';

console.log(apiUrl);

Keep in mind that, since we're in a REPL (fill in the blank: R__d E______e P___t L__p), you don't have to use console.log to print the contents of our variable to the Console. Our Console will do it for us, so you can just type apiUrl and hit enter. I am choosing to use console.log however, so we're not confused as to why nothing happens when we run our app externally later and not within our REPL.

In JavaScript, it is advised that each expression, or instruction which yields a value, is followed by a semicolon (;) when it's on its own so that the computer doesn't get confused when executing them in case they end up on the same line.

In our previous example, I chose to call our variable apiUrl because I wanted a name descriptive of what it holds inside. Try to avoid using non-descriptive variable names such as x or au since you will most likely forget what it holds when your application gets bigger and it will make it harder for you or someone else to debug.

The reason why I capitalized the U of Url in apiUrl is because we're not allowed to have spaces in our variable names. If we used a space, the computer would mess up and evaluate our variable name as two different instructions. No bueno. However, I could have used an underscore like this, instead:

var api_url = '...';

But the convention in JavaScript is to use what's called the camel case notation which means capitalizing the first letter in each word (except for the first one). This obviously looks like a camel only if you were blind: thisDoesNotRemindMeOfCamels.

So what are variables exactly?

Variables are just containers in our computer's memory that hold values. Well in reality it's a bit more complex, but for now that should be a sufficient explanation.

So think of variables this way: I live in Brooklyn, NY. My apartment is the container; Peleg, the person (that's me), is the value, and the address for it in the world is 241 S3rd St. Apt. 18, Brooklyn, NY 11211, USA (don't come kill me). Something like that at least.

Now if you were to change the value of the apartment and put my mom's dog, Charlie, in it instead of me, then now the same address would point to Charlie the dog instead of Peleg the human. So in our previous example, if we were now to change apiUrl's value like this:

apiUrl = 'blah blah blah';

It would have this new vlaue and you could see it by printing it to the Console again:

console.log(apiUrl);

and it will print its new string, "blah blah blah". Don't forget to reinstate our initial URL if you're intending to continue using it.

By the way, you should note that we didn't precede our re-assignment with the keyword var here. That is because our variable was already declared earlier when we assigned it with the URL. Now we were just changing its value.

Cool. Now the stuff I'm talking about here most likely makes sense only half the time and it's perfectly okay. This is how most people learn and if all of this information made sense to you the first time you encountered it, then good for you, show off, but for the rest of you -- don't get discouraged by ambiguity. It is how I learn too -- just power through the confusing lines of this book and get back to them some other time.

Table of Contents
Home