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
camelCase
but it is not mandatory
Variable Declarations
- let's finally clear up the
const
andlet
keywords - 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
var
is the old school JS variable declaration- like automatic declarations, use of
var
is now frowned upon but some people still use it out of habbit and for consistency when maintaining old code bases (var
was the only variable declaration pre ES6 which came out in 2015) var
is frowned upon because it can cause scope issues (more on "scope" when we get intofunctions
)
var x = 13;
const
const
andlet
are the modern ways to declare variablesconst
stands for constant and declares variables that do not change- using
const
makes the variable read only - it is good practice to default to
const
for variables
const x = 13;
let
let
is how you create a variable that can be changed
let x = 13;
let x = 10;
let
is very semantic, read it as "let x equal thirteen"