Variables
| 📚 Table of Contents |
|---|
| var |
| const |
| let |
- variables are super important in programming. every language has them.
- variables are containers for storing data.
- variables are exactly what I think of when I hear the word "coding".
i.e:
const aMovie = "The Evil Dead 2";that is like saying "aMovie is code for the string The Evil Dead 2".
or
const x = 13;that is like saying "x is code for the number 13"
- variables can hold any data type including entire functions
JS is case sensitive
- this is a good time to mention this.
const adam = "Adam";
// these are different and unique variables
const Adam = "Adam";- the standard naming convention for variables in JS is
camelCasebut it is not mandatory
Variable Declarations
- let's finally clear up the
constandletkeywords - these are variable declarations, which let you define variables
- technically these are optional in JS but it is considered really bad practice not to declare variables. not declaring variables doesn't just make reading the code a little muddy, it also opens you up to a few different kinds of bugs. so, you really shouln't do this but since you can, you might see it in the wild. not declaring variables is automatic declaration and looks like 10th grade algebra:
x = 13;var
varis the old school JS variable declaration- like automatic declarations, use of
varis now frowned upon but some people still use it out of habbit and for consistency when maintaining old code bases (varwas the only variable declaration pre ES6 which came out in 2015) varis frowned upon because it can cause scope issues (more on "scope" when we get intofunctions)
var x = 13;const
constandletare the modern ways to declare variablesconststands for constant and declares variables that do not change- using
constmakes the variable read only - it is good practice to default to
constfor variables
const x = 13;let
letis how you create a variable that can be changed
let x = 13;
let x = 10;letis very semantic, read it as "let x equal thirteen"