4.4. More On Variables¶
The previous section covered creating, evaluating, and reassigning variables. This section will cover some additional, more nuanced topics related to variables.
4.4.1. Creating Constants With
One of the key features of variables that we have discussed so far is their ability to change value. We can create a variable with one value, and then reassign it to another value.
For example, suppose that we are writing a to-do list web application, named "Get It Done!" The title of the application might appear in multiple places, such as the title bar and the main page header.
We might store the name of our application in a variable so that it can be referenced anywhere we want to display the application name.
let appName = "Get It Done!";
This allows us to simply refer to the
appName variable any time we want to
use it throughout our application. If we change the name of the application, we
only have to change one line of code, where the
appName variable is
One problem with this approach is that an unwitting programmer might change the
appName later in the code, leading to inconsistent references to
the application name. In other words, the title bar and main page header could
reference different names.
const rather than
let to create a variable ensures that the value
of the declared variable cannot be changed.
const appName = "Get It Done!";
Such an unchangeable variable is known as a constant, since its value is just that.
const appName = "Get It Done"; appName = "Best TODO application Ever!";
TypeError: Assignment to constant variable.
4.4.2. Naming Variables¶
188.8.131.52. Valid Variable Names¶
let application name;
SyntaxError: Unexpected identifier
In this case, "identifier" is another term for variable name, so the error message is saying that the variable name is not valid, or is "unexpected".
Use only the characters 0-9, a-z, A-Z, and underscore. In other words, do not use special characters or whitespace (space, tab, and so on).
Do not start a variable name with a number.
Following these guidelines will prevent you from creating illegal variable names. While this is important, we should also strive to create good variable names.
184.108.40.206. Good Variable Names¶
Writing good code is about more than writing code that simply works and accomplishes the task at-hand. It is also about writing code that can be read, updated, and maintained as easily as possible. How to write code that achieves these goals is a theme we will return to again and again.
One of the primary ways that code can be written poorly is by using bad variable names. For example, consider the following program. While we haven't introduced each of the components used here, you should be able to come to a general understanding of the new components.
1 2 3 4
let x = 5; const y = 3.14; let z = y * x ** 2; console.log(z);
Understanding what this program is trying to do is not obvious, to say the
least. The main problem is that the variable names
not descriptive. They don't tell us anything about what they represent, or how
they will be used.
Variable names should be descriptive, providing context about the data they contain and how they will be used.
Let's look at an improved version of this program.
1 2 3 4
let radiusOfCircle = 5; const pi = 3.14; let areaOfCircle = pi * radiusOfCircle ** 2; console.log(areaOfCircle);
With improved variable names, it now becomes clear that the program is calculating the area of a circle of radius 5.
When considering program readability, think about whether or not your code will make sense to another programmer. It is not enough for code to be readable by only the programmer that originally wrote it.
220.127.116.11. Camel Case Variable Names¶
There is one more aspect of naming variables that you should be aware of, and that is conventions used by professional programmers. Conventions are not formal rules, but are informal practices adopted by a group.
In the United States, it is common for two people to greet each other with a handshake. In other countries and cultures, such as some in east Asia, the conventional greeting is to bow.
Failing to follow a social convention is not a violation of the law, but is considered impolite nonetheless. It is a signal that you are not part of the group, or do not respect its norms.
There are a variety of types of conventions used by different groups of programmers. One common type of convention is that programmers that specialize in a specific language will adopt certain variable naming practices.
are joined together to omit spaces,
start with a lowercase letter, and
capitalize each internal word.
In the example from the previous section, the descriptor "area of circle" became the variable name
areaOfCircle. This convention is called camel case because the capitalization of internal words is reminiscent of a camel's humps. Another common name for this convention is lower camel case, since names start with a lowercase letter.
Different programming languages often have different variable-naming conventions. For example, in Python the convention is to use all lowercase letters and separate words with underscores, as in
We will use the lower camel case convention throughout this course, and strongly encourage you to do so as well.
Attempting to use a keyword for anything other than it's intended use will result in an error. To see this, let's try to name a variable
let const ^^^^^ SyntaxError: Unexpected token const
Most code editors will highlight keywords in a different color than variables or other parts of your code. This serves as a visual cue that a given word is a keyword, and can help prevent mistakes.
We will not provide the full list of keywords at this time, but rather point them out as we learn about each of them. If you are curious, the full list is available at MDN.
4.4.4. Check Your Understanding¶
Which is the best keyword for declaring a variable in most situations?