Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Quickstart!
You have completed JavaScript Quickstart!
Preview
JavaScript provides a way to store and keep track of information in a program with variables. Variables are one of the most important concepts in JavaScript, and programming in general.
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
Now, just printing the return value to the
console like this isn't all that useful.
0:00
Wouldn't it be great if we could also
store and use this value in our program,
0:05
like post it to a list of users or
display it somewhere on the page?
0:09
Well, JavaScript provides
a way to store and
0:13
keep track of information in
a program with variables.
0:15
Variables are one of the most
important concepts in JavaScript and
0:18
programming in general.
0:21
For example,
0:23
I'll use a variable to store the message
to display in a prompt dialogue box.
0:24
To declare a variable,
0:29
you use the key word var followed by
the name you want to give the variable.
0:31
I'll name it question.
0:36
Then I'll assign a value to this variable.
0:40
We have an equal sign and
I'll put a string value into it.
0:42
The message will be, What is your name?
0:46
This is called assigning a value or
0:51
putting a value Into the variable,
so now I'll press
0:54
shift+enter to bring the cursor down to
the next line without running the code.
0:58
I'll type the new prompt command, and
instead of passing prompt a message as
1:04
a string, I'll type the variable name
question between the parentheses.
1:09
I'll press Enter to run the code, and
1:15
in the browser I get the same message
as before in the dialogue box.
1:18
But now if I use the question
variable anywhere in my program,
1:22
the value for question will always
point to the string, What is your name?
1:27
You can think of a variable
as a labeled box.
1:33
You can put something inside the box.
1:36
Look inside the box to
find out what's in it.
1:38
You can empty the box.
1:40
Even put something new in the box.
1:41
While the contents of the box changes,
1:43
it's still always the same box
with the label you gave it.
1:45
It's common for
a variable's value to change in your code.
1:49
That's why they're called variables.
1:52
For example,
say you are creating a computer game and
1:53
using a variable named score to
keep track of a player's score.
1:56
At the start of the game,
the initial value for score is 0 but
2:00
it can go up if the player does well or
go down if the player makes a mistake.
2:04
Although the value for the value for
the score variable changes from one to
2:08
a higher or lower number, it's still
always just one score for that player.
2:12
Nowadays, you'll see variables
written using the keywords let or
2:17
const which let you put a value into
them like you do with the var keyword.
2:20
But they different rules
about how you use them and
2:25
change their values in your program.
2:28
For now, I'll stick with the var keyword
2:30
since it's easier to remember
that var is a variable.
2:33
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