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
Using the keyword `const` to create variables have a number of benefits including the ability to prevent the value from being reassigned.
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 first method you should consider when
creating a variable is the const keyword.
0:00
This is often the best choice for
declaring variables.
0:06
Const is short for constant, meaning
that the value of the variable shouldn't
0:09
change, it should remain constant.
0:13
For example, const pi is equal to 3.14159.
0:16
Const lets you protect variables from
being overwritten by any stray assignments
0:21
in your code.
0:26
In other words, once you create a
constant, you can't give it another value.
0:27
Let me show you what I mean.
0:32
If you want to follow along, you can
open up the workspace on this page.
0:34
Open up pi.html.
0:39
And as you can see, we've assigned
the value of 3.1459 to the constant pi.
0:43
Click on preview and navigate to pi.html.
0:51
Open up the developer tools and
head over to the job script console.
0:56
When we type pi, The value is locked.
1:01
Try to assign another value to pi.
1:05
We see we get an error.
1:12
TypeError: Assignment
to Constant Variable.
1:13
You will use constants frequently,
in fact,
1:17
it should be your first choice
when declaring variables.
1:19
You will use them to store numbers that
shouldn't change when your program runs.
1:22
For example, tax rates, dimensions
of user interface components and
1:26
product prices are good examples.
1:31
And you'll use constants when you're
selecting elements on a page.
1:33
Or when you're assigning
a function to a variable.
1:38
Any value can be assigned to a constant.
1:41
However, for a variable whose value will
change during the life of a program,
1:44
const are a bad choice.
1:49
For example, you wouldn't want to
const to store score of a game.
1:51
Since the score changes as
the player plays the game.
1:55
In that case, you'll use the let keyword.
1:59
I'll show you how that
works in another video.
2:02
As I mentioned,
constants help you fix problems
2:05
that can easily crop up in
your JavaScript programs.
2:08
Let's take a look at a common
problem that you'll run into.
2:12
If you want to follow along, open up the
name.html file in this video's workspace.
2:15
Here's a simple script
with a variable name.
2:22
I'll preview this file and
check in the console.
2:25
Notice that the console says Andrew.
2:33
That's the value of the name
variable defined on line one.
2:36
The console then prints out Joel Kraft
2:40
which is logged by
the createFullName function.
2:44
Now that the program has run,
what's the value of the name variable?
2:49
I will just check that out in
the console by writing name.
2:54
It's now Joel Kraft.
2:58
Andrew is gone.
3:00
The variable has been
overwritten by the function.
3:02
This is a simple scope issue.
3:06
The function is overwriting the name
variable in the global scope.
3:09
It's a problem that can pop up and
can be difficult to catch.
3:14
However, const will help
you spot the mistake.
3:18
Now when I preview this,
The console spits out an error.
3:24
You can't change the value of a constant.
3:33
Because I use const, I can see that
the simple mistake I made, and
3:36
I can go back into my script and fix it
3:40
Simply adding const before
the name within the function,
3:48
keeps that variable locked to
the scope of the function.
3:51
It's no longer trying to
reassign the name variable.
3:55
I'll preview this script one more time.
3:59
And no errors, and I can logout name,
and it's still untrue, hurray!
4:03
I'm not gone.
4:09
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