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