
Level: Beginner — Introductory
You may think back to your middle school pre-algebra class when you hear the word variable. Variables are just as important to programmers as they are mathematicians. While it is possible to write JavaScript without always using variables, I would not recommend it. I like to think of variables like ink stamps: they are created to hold a value and can be reused across a program, similar to stamping multiple papers with one stamp. Where this analogy breaks down is that variables can be updated, ink stamps really can’t (as far as I know). Values are stored in variables — things like numbers and strings of text.
You an use values independent of variables, but storing values in variables is almost always more effective.
Here are the types of primitive data values you may decide to store in a variable:
- A string of text like “Sally’s Dog”
- The number 529 (can be a decimal or integer)
- BigInt are large integers that a number type cannot hold (2 to the 53rd power minus 1!)
- Logical Boolean values: True or false
- Undefined are values taken by a variable that are yet to be defined (an empty value)
- Null which is also an empty value, but usually null means an object is missing, where with undefined the object just hasn’t been initialized yet. Here is a good summary of null”: https://dmitripavlutin.com/javascript-null/
- Symbol is a value that is unique and can not be changed
Unlike programming languages, such as Java, you do not need to declare the data type-JavaScript figures it out on its own!
So before you start writing variables, it is important to make sure you writing them in a way that others will understand. This means writing your variables using camelCase. For example, lets say I want to store a string of text “Pizza” inside a variable. I am going to call the variable “lunchChoice”. Notice how the first letter in the variable is lowercase and the first letter of second word is uppercase. Also, the variable in this example is descriptive, it lets you know what it is holding.
There are often times when you will have to return to your code after much time has passed. You will be very thankful you gave your variables descriptive names in these circumstances. Make sure there are no spaces in your variable too! That is really all there is to camelCase. This will make reviewing code easy for others who do not have the context behind your variables. It is a convention, not a rule — but one best followed.
There are also a few ways that JavaScript will prevent you from writing variables:
- You cannot start variable names with a number. For example “4dogs”
- Aside from the underscore symbol( _ ) and the dollar sign ($), you cannot use symbols inside of a variable
- Certain keywords are reserved in JavaScript such as “double” or “void” See a complete list here: https://www.w3schools.com/js/js_reserved.asp
Before you get into creating your own code, consider committing some of these best practices to memory. This will help you build a successful foundation for coding.
References:
- Schmedtmann, The Complete JavaScript Course 2020
- Pavlutin, Everything null in JavaScript
- MDN Web Docs