Learn how variables are ways to name things—similar to math, but forget x and y.
We can label the box whatever we want. And put pretty much whatever we want in there. And we end up with a convenient way to refer to some data. Remember, that as a programmer, you're in charge. The computer is going to do just what you tell it to do. Nothing more, nothing less. You should assume the computer, basically, doesn't know anything unless you tell it something. Variables are an important part of how we do this. In the course of programming, we're dealing with a lot unknowns. At least a lot of things that are unknown at first. We then use variables to accumulate the knowledge we need to accomplish whatever the goals of our program are.
Here's an example. Say we want to write a function that's going to let someone draw a line on screen. Some of the things we don't know at first are the points where the line will start and end. We could have variables for those points and refer to them later by name when it comes time to draw the line. Okay, so that's the theory. Here's how it works. If we're approaching this the way you would approach it in math class, you'd have variables called things like x. That's enough to create a variable but it's an empty box right now. To give this variable x, a value, we add a space then an equal sign.
But in the console I can type the name of the variable I just created again and do nothing else except end the line with a semicolon and I get back the value I just assigned. And in Chrome, it actually preemptively executes this for me and gives me a little preview of what will happen if I were to execute this. So I hit Return and there's 32 again. Calling things x is something that gets a lot of complaints in math classes. And we can certainly do better in our programs. Variables can be named almost anything that starts with a letter, underscore, or dollar sign. So I can create a variable called whereAmI.
And I can change whereAmI, I start to type this, and then I complete it by hitting Tab. An equal sign and again, Chrome is previewing what's actually in there right now. I can change it to Los Angeles. Or, I could change it to 75. I did this by hitting up from a blank line to get the last thing that I typed. So whereAmI, set that to 75. I can set it to whatever I want.
So we'll say monster1 equals Grover. Now I'll type a comma. Moster2 is Cookie Monster. Monster3 is Animal. End that with a semicolon. This just saves me a little bit of typing without having to do all these on separate lines. If I type each variables name and hit Return, I get exactly what I put in.
A couple more details. Your variables can be named almost anything you want. And I recommend you use names that are descriptive because programs aren't just for computers. They're for other humans to read as well. A computer isn't going to care what you call your variables for the most part, but humans definitely will. So make sure you name your variables something that indicates the sort of thing that they're actually going to contain. If I were writing a program dealing with how much it costs to ship a package, for example, I might have variables named things like packageDimensions. Or taxRate. And so on.
- Using a text editor
- Declaring and assigning variables
- Booleans and the quest for truth
- Working with objects and arrays
- Using operators and control structures
- Iterating with loops
- Objects, references, and functions
- Promises, async, and await