Variables help store information and reference it multiple times.
What are variables?
Do you remember learning about variables back in algebra one?
A variable in algebra is a letter used to represent a number, and to be used in a expression.
i = 2 i + 3 = 5
In the example above, we used the variable
i to represent the number 2.
We can use
i to calculate the total to be 5.
The same goes for variables in programming. You may declare variables to allow you to reference the same piece of information multiple times.
var price = 5; var items = 3; var total = price * items; console.log(totals) // 15
var first followed up with the variable name.
var x = 5;
In the example above,
x is the variable identifier, and it has a value of 5.
var foo = 2; console.log(foo); // Output is 2 foo = 9999; console.log(foo); // Output is 9999
Look at the code above, I declared a variable called
foo and assigned it a value of 2. I then printed the ,
foo and it showed me the correct value, 2.
After the first print, I than changed the value of
foo to 9999. I then printed
foo again, and it showed me the new value I gave it, 9999.
Another thing to note is that I didn't re-type the keyword
var again. Once you declare a variable once, you don't need to do it again.
var, but you also have
var has been available since 1995, but with the release of ES6,
const are now available to declare variables.
var foo = 2; let bar = 2; const age = 29; console.log(foo); // 2 console.log(bar); // 2 console.log(age); // 29
Each one of these special keywords allow you to declare a variable.
You might be asking yourself, "what's the difference between all 3?"
Or "Which one should I use?"
// Functional scope var price = 45; price = 100; price = 0;
var allows you to change the value at anytime in program.
var is also functional scope. This topic will be discussed in further details later in this guide.
// Block scoped let price = 10; price = -100;
let is similar to
var with the exception that block scoped and NOT functional scoped.
let isntead of
const price = 5; // JS application will break! price = 10;
const is similar to
let but with the exception that it's a fix pattern through the entire application.
const also stands for constant.
// Example with 'var' var price1WithVar = 15; console.log(price1WithVar); // 15 price1WithVar = 0; console.log(price1WithVar) // 0. It's free! // Example with 'let' let price2WithLet = 45; console.log(price2WithLet); // 45 price2WithLet = 10; console.log(price2WithLet); // 10 // Example with 'const' // Variable value may NOT change const age = 29; console.log(age); // 29 age = 40; // Error. JS breaks.
Try to use
const as much as possible.
Variable naming patterns
Variable names are very important in programming, because it will help with readability, and understanding of your code. This will help future you, and your team members.
My rule of thumb is to always make your variable names to be pronouns.
// Bad: Never start with a number or special character. const 1foo = 2; // Bad: Don't do arbitrary letters const x = 45; // Good: const saraAge = 25; // Good const hisAge = 13;
Pick one of following patterns, and stick with it until the end of the program. Consistency is key to good code.
// Camel case const momHeightInInches = 65; // UpperCamel case const DadHeightInInches = 69; // Snake case const my_height_in_inches = 67;
- All variables must be unique names
- Pick descriptive names
- Variables may only contain letters, digits, underscores, and dollar signs
- Variables must begin with a letter, $, or _
- Variables are case sensitive
- Reserved keywords cannot be used
- Pick a pattern such as
constby default, unless a variable needs to be reassigned
varkeyword should not be used
- Declare you variables as early as possible