What you'll learn:
let
, const
, and var
let
In the last post, we learned that we can create a variable with the let
keyword and how a variable is like a labeled bucket which we can put things in.
There are also two other keywords we can use.
const
If you read other peopleβs code, you might see const
instead of let
.
function sandwichMaker(meat){
const sandwich = "π"
sandwich = sandwich+meat
sandwich = sandwich+"π"
sandwich = sandwich+"π"
return sandwich
}
This defines a constant instead of a reassignable variable.
Similar to variables, a constant is a labeled bucket too. We can use its value by the name. However, unlike a variable, once we put a value into a constant, we cannot change it. Thatβs why it's called a constant, and why we get errors in these lines.
function sandwichMaker(meat){
const sandwich = "π"
// Error "sandwich is read-only and cannot be reassigned
sandwich = sandwich+meat
sandwich = sandwich+"π"
sandwich = sandwich+"π"
return sandwich
}
You might ask whatβs good about a constant if we cannot change its value. Well, just like many things in life, adding some limitations can bring benefits. This barrier can be exactly what we want. It can make the code easier to read and help us avoid mistakes because we would not need to worry about accidental changes to the values.
For example, if you are dealing with a physics game, you might need to add some physical constants such as gravity that we don't want to accidentally change.
var
If you have studied JavaScript before, you probably know thereβs another keyword to define variables. Itβs var
instead of let
function sandwichMaker(meat){
var sandwich = "π"
sandwich = sandwich+meat
sandwich = sandwich+"π"
sandwich = sandwich+"π"
return sandwich
}
var
vs let
Similar to let
, this defines a variable sandwich
. We can use its value by the same name. We can also change the value of the variable with an equal sign. However, there are some subtle differences between the variables defined by let
and the ones by var
. But letβs not worry about it at the moment.
You can consider let
as a better way to define variables than var
. Weβll always use let
instead of var
.
In this function, the parameter meat
is, in fact, a variable too.
function sandwichMaker(meat){
let sandwich = "π"
sandwich = sandwich+meat
sandwich = sandwich+"π"
sandwich = sandwich+"π"
return sandwich
}
You can think of it as if thereβs a hidden let
statement at the beginning of the function. It initializes the variable with the value we pass in when calling the function.
function sandwichMaker(){
let meat = "π₯"
let sandwich = "π"
sandwich = sandwich+meat
sandwich = sandwich+"π"
sandwich = sandwich+"π"
return sandwich
}
let someVariable = sandwichMaker()
This would essentially be equivalent to
function sandwichMaker(meat) {
let sandwich = 'π'
sandwich = sandwich + meat
sandwich = sandwich + 'π'
sandwich = sandwich + 'π'
return sandwich
}
let someVariable = sandwichMaker('π₯')
However, the first method can't change because it doesn't have any input anymore!
That is let
, const
, and var
! Remember we'll be using let
instead of var
! We'll learn about a special function called onTap
next!