As in most programming languages, you bind a name to value with a what’s called variable statement. Generally speaking, variable statements have three items:
(1)KEYWORD (2)NAME = (3)VALUE
Here’s how the code may look in real life:
const learningTheBasics = true;
In the code above:
- “const” is the special function KEYWORD
- “learningTheBasics” is the variable NAME
- “true” is the VALUE
Let’s explore those one at a time:
1. Variable keywords.
Any keyword you choose will declare a variable, but they behave differently. Generally speaking, I use const by default, let when required, and var … never. Let’s ignore var for now because you shouldn’t likely be using it.
What’s the difference between const and let?
- const stands for “constant” and cannot be reassigned a different value (in other words, it’s a way of permanently assigning a name to a value)
- let can be reassigned a new value later in your program
By using const as the default, it ensures you don’t accidentally overwrite a reassignable variable (i.e., let). If you try to reassign const, your program will throw an error. If you meant to reassign it, you can change the keyword to let; and if you didn’t want to reassign it, it’s a good thing you used const. 😏
2. Variable names.
The name is sometimes called the “identifier”; it’s what you use to speak about the variable later in your code. Before we get crazy, let’s start with some basic rules.
- start with a number
- have a space between words
So these would all be invalid variable names:
const 1stLearner = 'me'; // “1stLearner” starts with a number const var = 'variable'; // “var” is a reserved keyword const my name = 'Chris'; // “my name” has a space
- begin with a letter (upper or lowercase), the dollar sign symbol ($), or the underscore symbol (_).
- only contain letters, numbers, underscores, or dollar signs
Best practices for naming variables
How should you name variables? Here are the generally-accepted best practices:
- Use “camelcase” to name variables.
Camelcase means the variable name starts lowercase, but every additional word in the name starts with an uppercase letter. Because variable names are case-sensitive, the most important practice is to be consistent. Here’s an example without camelcase:
Here’s the same example with camelcase:
const listofnamesfromserver = [‘jim’, ‘joe’, ‘pearl’, ‘pam’]
Camelcase makes variable names easier to read.
const listOfNamesFromServer = [‘jim’, ‘joe’, ‘pearl’, ‘pam’]
Note: There’s one generally-accepted exception: true constants. If you follow my suggestion above (that you should use const by default), most of your variables will be const declarations, even though they don’t have to be constants in most instances. When people follow this practice, they usually fully CAPITALIZE variable names that must be constants for the program to work (e.g., you couldn’t change it to “let” without breaking your program).
- Write for humans to understand.
Optimization and speed is important, but so is upkeep and readability. While “x” is a fine name for a variable, it doesn’t tell you anything about what the variable points to and thus requires more work to understand. Later on, you can use tools to shorten and optimize your variables upon build. For now, be very descriptive.
3. Variable values.
- String: surrounded by ``, ‘’, or ""
- Number: a number (either an integer or decimal point)
- Boolean: true or false (not surrounded by ``, ‘’, or "" —> because that would be a string)
- Null: absence of a value
- Undefined: a special word meaning a variable or object is initialized but has no value
- Symbol: a special value that always returns a unique identifier
Note: The assignment operator (i.e., how you assign the value to the variable) is a single equals sign. You can also update let or var variables with the equals sign as shown below.
How to Declare a Variable
To pull everything together, you declare a variable how we started:
(1)KEYWORD (2)NAME = (3)VALUE
There’s one more difference to note here between const and let:
- With const, you must provide a value
- With let, you can merely initialize it (e.g., “let firstName;”) and then provide a value later using only the name (because let can be updated later);
const firstName = 'Jenny'
This 👆 uses const, and must be assigned a value when you initialize it (e.g., you cannot merely type const firstName; because you can’t update a const variable later.).
let firstName; firstName = 'Jenny'
This 👆 first initializes a let variable named firstName and then later updates the value by referencing only the name (e.g., firstName).