Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Practice working with let and const variables in JavaScript.
More about Let and Const
Define Variables with let
and const
More about Objects
Intro to Template Literals
String Interpolation with Template Literals
Spread Operator
Copy and Combine Arrays with the Spread Operator
Hint: Unlike var
variables, let
and const
variables cannot be accessed before they are declared. Do you see anywhere toward the bottom of the code where this might be happening?
[MUSIC]
0:00
Hey there.
0:09
Ashley here.
0:10
Today we're going to be
Practicing Working With Const and
0:11
Let Variables in JavaScript.
0:14
This is a follow up to the first part of
the Getting Started With ES 2015 Course.
0:16
Defining variables with let and const.
0:21
If you haven't finished that section yet
or are unfamiliar with const and
0:24
let variables, now might be a good
time to go and review those videos.
0:28
I've included a link to them
in the teacher's notes.
0:32
In this practice session,
0:35
you'll be writing a function,
editing a function, and
0:36
changing some other code to help you get a
feel for how const and let variables work.
0:39
And how they're different from
variables declared with a var keyword?
0:44
There's a workspace attached to this
video that includes the file you'll
0:48
be working out of with some
more detailed instructions.
0:51
You'll use the workspace console and
node to run your code.
0:54
Node, if you haven't used that before
is a way to run JavaScript code
0:58
outside of your browser.
1:02
This workspace is all set up to use node.
1:03
So when you're ready to run your file,
open up the work space console,
1:06
type node followed by a space and
then the name of your file.
1:10
In this case, let_const.js then hit Enter.
1:16
There's a note on how to do this
in the instructions as well.
1:22
Go ahead and
open up the let_const.js file.
1:25
Inside you'll see here instructions
written as JavaScript comments
1:29
at the top of the file.
1:33
After these instructions,
you'll see three variable declarations.
1:35
The first is teachers,
an array containing one object.
1:39
Object should be familiar to you from
the JavaScript Loops, Arrays and
1:43
Objects course.
1:47
The second variable
declaration is courses.
1:49
An array containing six
JavaScript course titles.
1:52
Finally, we've declared i.
1:57
A variable that's equal to
the length of the courses array.
1:59
After these variable declarations, you'll
see a function called add new teachers.
2:04
That takes one parameter, newTeachers.
2:09
But whoops, something's missing here.
2:12
Our addNewTeachers function doesn't
have any code inside of it.
2:15
Your first goal is to complete this
function by writing some code that lets us
2:18
add new teachers to our teachers array.
2:22
After the addNewTeachers function,
2:25
you'll see a function called
printTreehouseSummary.
2:27
This function runs but it's outputting an
incorrect number of treehouse, teachers,
2:30
and courses.
2:34
Use what you know about let and const
in JavaScript to fix this function so
2:36
it logs the correct
numbers to the console.
2:41
Now don't worry if the string format used
in the console.logs are unfamiliar to you.
2:44
I'm using something called template
literals which offer a more concise and
2:49
simple way to display
string data in ES2015.
2:53
You'll learn all about template
literals in the next segment of
2:56
the Getting Started With ES2015 Course.
2:59
If you are to run your code at this point,
you would get an error message.
3:03
Your final goal for
3:07
this practice session is to figure out
what's causing the error and fix it.
3:08
The error message in the consul should
give you some clues about where to start
3:12
and if you're really stuck check
the teacher's notes for a hint.
3:15
Once you've completed this three tasks
you should be able to run your code.
3:19
Now remember to run your code you're gonna
type node, then the name of the file,
3:24
which is let_const.js and then hit Enter.
3:28
Now if your code works correctly,
your output should look just like this.
3:33
If you think you're ready to get started
go ahead and try to program your solution.
3:38
If you're having trouble,
that's 100% okay.
3:43
Practicing, trying new things,
and playing around with code,
3:46
is part of how we learn, and
is a vital part of programming.
3:49
Either way, come back and watch video two
to check your work and see my solution.
3:53
If you're looking for
some more background info,
3:57
don't forget to check the teacher's
notes for links to related coursework.
4:00
See you soon.
4:03
You need to sign up for Treehouse in order to download course files.
Sign up