Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Welcome to Getting Started With ES2015 1:01
- Declaring Variables in JavaScript 1:20
- Creating Unchanging Variables With const 4:10
- Using Constants with Arrays and Objects 2:44
- Review creating variables with const 5 questions
- Defining a Variable with let 2:48
- Using let with for Loops 2:25
- Reviewing let and const 1:00
- Review let and const 5 questions
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Using constants is are a best practice when assigning variables but what happens when you do actually want to reassign a value? That's where let comes in.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The let keyword is used when you
want to re-assign a variable.
0:00
In that respect, it just works like var.
0:04
Let's take a look at a simple example.
0:07
Open up let.html, and you can see
we've got a score that equals 0,
0:10
an age that equals 33, and a Boolean,
isHungry, that equals false.
0:15
Open up the preview, and
navigate to let.html.
0:22
Open up the Console, And
let's try and modify the score.
0:29
The score can change through a game,
0:36
the score increments by one each time
the player completes a challenge.
0:38
Again, let just works like var,
0:43
you can re-assign values to a variable
that is declared with the let keyword.
0:47
You can update the age to be 34, and
0:53
isHungry, To be true.
0:58
Let's look at a more complex example,
1:08
where you should use let
rather than a const.
1:11
Open up person.html, and here we have
a function called personDescription.
1:14
That creates a description string
that gets logged out at the end here.
1:21
If the person object has a role,
1:27
It will add additional information at the
end of the description, and is logged out.
1:33
Let's see this in action.
1:40
Go to the preview, click on person.html,
1:42
open up the developer console, and
we should see Andrew is a Teacher.
1:46
If we change the var keyword in
the person description function to const,
1:52
And run it again, we get an error,
1:59
because we're trying to re-assign
the description in the if block.
2:01
We could change it to var,
2:10
or alternatively we could
use the new let keyword.
2:12
Let works like var,
allowing you to re-assign variables, but
2:18
unlike var it has block level scoping.
2:23
We'll explore when we'd see the advantages
of using let over var in the next video.
2:26
Now that we've changed
the constant to let,
2:32
let's see how it works in our browser.
2:34
Looking in the console, we see Andrew is
a Teacher printed out like we expect.
2:38
In the next video,
2:42
we'll look at how let solves some problems
where the var keyword falls short.
2:43
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up