let
as in "Let there be light" or "Let X equal 10".let color = "blue"
let fruit = "berry"
color + fruit // "blueberry"
fruit.toUpperCase() // "BERRY"
let
is just one way to declare a variable in JavaScriptvar
is a lot like let
but has wider scope which is sometimes badlet
or var
then the variable becomes global (which is dangerous)Unfortunately, in JavaScript you can only use let
once per variable name (in a given scope), otherwise you will get an error:
Identifier 'x' has already been declared
That means that when you're in the JavaScript console, if you see this error then try again without the let
> let x = 1
undefined
> let x = x + 2
SyntaxError: Identifier 'x' has already been declared
> x = x + 2
3
let
is undefined
, but the value of a normal assignment is the value being assignedThink of memory as a giant warehouse.
Like this warehouse from the movie Raiders of the Lost Ark, computer memory is vast and filled with boxes of various sizes.
If memory is a giant warehouse...
...and memory locations are boxes in that warehouse
...then a value is the contents of a box
...and a variable is a label you stick on the outside of the box
Which is clearer, this:
60 * 60 * 24
or this:
let secondsPerMinute = 60
let minutesPerHour = 60
let hoursPerDay = 24
let secondsPerDay = secondsPerMinute * minutesPerHour * hoursPerDay
?
Let's spend a few minutes just playing around with variables in the JavaScript console.
Some things to try:
let snack = "Apple"
Think of a variable as an arrow pointing to a value.
You can assign and reassign variables at will.
color = "blue" // assign 'blue' to color
fruit = "berry" // assign 'berry' to fruit
color + fruit // 'blueberry'
color = "black" // 'black'
color + fruit // 'blackberry'
Reaassignment only changes the name of an object. It does not change the data inside the object.
This is analogous to removing a label from one box and placing it on a different box.
Tip: Did you get an Identifier 'color' has already been declared
error? Try again without the let
, or restart your JavaScript console (in a Browser, Reload the page; in a Terminal, quit and relaunch node
).
let fruit = "Apple"
let snack = fruit
After this both snack
and fruit
are pointing to the same value
This is analogous to placing two labels on the same box.
Most messages return new values:
let fruit = "banana"
let snack = fruit.toUpperCase()
"banana"
and "BANANA"
are two different values in memory. The original value is still sitting around and still pointed to by fruit
.
Most messages do not change the data inside the object.
let color = "blue"
color.toUpperCase() // "BLUE"
color // "blue"
This is true for all strings, since strings in JavaScript are immutable. Any message that transforms a string will return you an entirely new string.
But some messages do change the contents!
Let's say we have a friend named Joe and his birthday is Independence Day, 1990.
We will use the built-in JavaScript Date
type to represent a year+month+day.
let independenceDay1990 = new Date(1990, 6, 4)
independenceDay1990.toDateString() // 'Wed Jul 04 1990'
let joesBirthday = independenceDay1990
Then we learn that Joe's birthday is actually Bastille Day. No problem, we'll just tweak the variable.
joesBirthday.setDate(14)
joesBirthday.toDateString() // 'Sat Jul 14 1990'
But what happened to the original date?
independenceDay1990.toDateString() // 'Sat Jul 14 1990'
Oops! Our program now thinks Independence Day 1990 was on July 14. This is a problem. What's the solution?
const
is just like let
, but also prevents reassignment
const pi = 3.14159;
const
is constant after it's been set once
pi = 7;
TypeError: Assignment to constant variable.
WARNING:
const
prevents reassignment but does not prevent changing the insides of objects (like the dates in the previous slide).
/