1 00:00:00,340 --> 00:00:05,660 Now, just printing the return value to the console like this isn't all that useful. 2 00:00:05,660 --> 00:00:09,660 Wouldn't it be great if we could also store and use this value in our program, 3 00:00:09,660 --> 00:00:13,460 like post it to a list of users or display it somewhere on the page? 4 00:00:13,460 --> 00:00:15,950 Well, JavaScript provides a way to store and 5 00:00:15,950 --> 00:00:18,740 keep track of information in a program with variables. 6 00:00:18,740 --> 00:00:21,930 Variables are one of the most important concepts in JavaScript and 7 00:00:21,930 --> 00:00:23,460 programming in general. 8 00:00:23,460 --> 00:00:24,260 For example, 9 00:00:24,260 --> 00:00:29,920 I'll use a variable to store the message to display in a prompt dialogue box. 10 00:00:29,920 --> 00:00:31,450 To declare a variable, 11 00:00:31,450 --> 00:00:36,270 you use the key word var followed by the name you want to give the variable. 12 00:00:36,270 --> 00:00:37,510 I'll name it question. 13 00:00:40,030 --> 00:00:42,610 Then I'll assign a value to this variable. 14 00:00:42,610 --> 00:00:46,530 We have an equal sign and I'll put a string value into it. 15 00:00:46,530 --> 00:00:49,480 The message will be, What is your name? 16 00:00:51,680 --> 00:00:54,370 This is called assigning a value or 17 00:00:54,370 --> 00:00:58,925 putting a value Into the variable, so now I'll press 18 00:00:58,925 --> 00:01:03,510 shift+enter to bring the cursor down to the next line without running the code. 19 00:01:04,520 --> 00:01:09,160 I'll type the new prompt command, and instead of passing prompt a message as 20 00:01:09,160 --> 00:01:13,750 a string, I'll type the variable name question between the parentheses. 21 00:01:15,530 --> 00:01:18,000 I'll press Enter to run the code, and 22 00:01:18,000 --> 00:01:22,300 in the browser I get the same message as before in the dialogue box. 23 00:01:22,300 --> 00:01:27,140 But now if I use the question variable anywhere in my program, 24 00:01:27,140 --> 00:01:31,090 the value for question will always point to the string, What is your name? 25 00:01:33,710 --> 00:01:36,360 You can think of a variable as a labeled box. 26 00:01:36,360 --> 00:01:38,090 You can put something inside the box. 27 00:01:38,090 --> 00:01:40,130 Look inside the box to find out what's in it. 28 00:01:40,130 --> 00:01:41,330 You can empty the box. 29 00:01:41,330 --> 00:01:43,430 Even put something new in the box. 30 00:01:43,430 --> 00:01:45,450 While the contents of the box changes, 31 00:01:45,450 --> 00:01:49,080 it's still always the same box with the label you gave it. 32 00:01:49,080 --> 00:01:52,040 It's common for a variable's value to change in your code. 33 00:01:52,040 --> 00:01:53,830 That's why they're called variables. 34 00:01:53,830 --> 00:01:56,770 For example, say you are creating a computer game and 35 00:01:56,770 --> 00:02:00,220 using a variable named score to keep track of a player's score. 36 00:02:00,220 --> 00:02:04,370 At the start of the game, the initial value for score is 0 but 37 00:02:04,370 --> 00:02:08,690 it can go up if the player does well or go down if the player makes a mistake. 38 00:02:08,690 --> 00:02:12,360 Although the value for the value for the score variable changes from one to 39 00:02:12,360 --> 00:02:17,180 a higher or lower number, it's still always just one score for that player. 40 00:02:17,180 --> 00:02:20,990 Nowadays, you'll see variables written using the keywords let or 41 00:02:20,990 --> 00:02:25,480 const which let you put a value into them like you do with the var keyword. 42 00:02:25,480 --> 00:02:28,220 But they different rules about how you use them and 43 00:02:28,220 --> 00:02:30,180 change their values in your program. 44 00:02:30,180 --> 00:02:33,360 For now, I'll stick with the var keyword 45 00:02:33,360 --> 00:02:36,320 since it's easier to remember that var is a variable.