Before we continue, it's imptorant to know that there's no room for error or ambiguity in programming. If you have some spelling mistakes in your essay, for example, chances are the reader will understand what you meant to say. But a computer only does exactly what you tell it to do—no more, no less. So when you make a mistake, the computer can't assume what you really wanted it to do. It either does the wrong thing or doesn't work at all.
Once the console is open, you can view the value of variables by simply typing the variable name. You can also change the value of variables and even declare new ones.
var name = "Duncan";
The console should then print this message:
Readability is very important when programming because you'll be frequently going back and changing previously-written code. The first step to making code easy to understand is naming variables and functions logically and consistently. Take a look at the following examples and see if you can figure out which option makes more sense:
var x = "Dylan Thomas";
var username = "Dylan Thomas";
var total = x + y;
var variable = x + y;
function myFunction(n1, n2);
function addNumbers(numberOne, numberTwo);
Names can also contain numbers, underscores and dollar signs, though this is usually frowned upon.
In addition to naming things well, adding comments to code can help to explain its purpose. This makes reading code much easier, especially when dealing with larger programs. Here's how you can add comments:
// This is a single comment line.
This is a multi-line comment.
It continues until it's closed.
Programmers have different opinions on where and how to add comments. Just remember that code usually makes sense as you're writing it, but it may not be as obvious later on.
Don't worry if you don't understand some of these concepts, we'll discuss variables and functions in greater detail in later lessons.