1 00:00:01,140 --> 00:00:03,020 It's a lot of fun to program, but 2 00:00:03,020 --> 00:00:07,410 it's not always fun to return to a program you wrote six months or a year ago. 3 00:00:07,410 --> 00:00:11,250 Looking at an old program sometimes feels like you're reading a foreign language, 4 00:00:11,250 --> 00:00:14,850 it's easy to forget why you programmed something the way you did. 5 00:00:14,850 --> 00:00:18,070 Your code is not just for the JavaScript engine to interpret, 6 00:00:18,070 --> 00:00:21,270 you should also consider other developers who might read and work with your code. 7 00:00:22,330 --> 00:00:26,020 Fortunately JavaScript provides a way to leave messages in your code 8 00:00:26,020 --> 00:00:28,890 that can help explain what's happening in a program. 9 00:00:28,890 --> 00:00:32,860 When you or another developer look at your code later, these messages or 10 00:00:32,860 --> 00:00:36,540 comments can quickly help describe what's going on in the program. 11 00:00:36,540 --> 00:00:39,015 You can add comments anywhere in your JavaScript code. 12 00:00:39,015 --> 00:00:42,760 They're ignored by web browser, and don't affect how your program works. 13 00:00:43,850 --> 00:00:45,310 Where should you put comments? 14 00:00:45,310 --> 00:00:49,430 Well it's a good idea to add comments to identify different sections of code. 15 00:00:49,430 --> 00:00:52,490 For example, to introduce a long conditional statement, or 16 00:00:52,490 --> 00:00:55,920 to explain particularly complex blocks of code. 17 00:00:55,920 --> 00:00:59,600 Comments not only help you, but also anyone else who works with your code. 18 00:01:01,270 --> 00:01:05,434 JavaScript provides two different types of comments, single-line comments and 19 00:01:05,434 --> 00:01:07,285 multi-line comments. 20 00:01:07,285 --> 00:01:11,060 Single-line comments are represented by two forward slashes, 21 00:01:11,060 --> 00:01:15,150 everything following the slashes until the end of the line is ignored by the browser. 22 00:01:15,150 --> 00:01:18,940 If you want to add a long message to your code, you can use a multi line comment, 23 00:01:18,940 --> 00:01:21,790 which begins with a forward slash and asterisk. 24 00:01:21,790 --> 00:01:25,610 Anything following those two characters, even carriage returns and multiple lines, 25 00:01:25,610 --> 00:01:30,100 are ignored by the JavaScript engine until it encounters a final asterisk and 26 00:01:30,100 --> 00:01:30,890 forward slash. 27 00:01:32,040 --> 00:01:35,380 I'll teach you how to use JavaScript comments by documenting 28 00:01:35,380 --> 00:01:38,799 the number guessing game you wrote in the file booleans.js. 29 00:01:40,360 --> 00:01:45,319 First, I'll add a multi-line comment that explains what the program does. 30 00:01:47,402 --> 00:01:52,507 The number guessing game stores a number between 1 and 10, 31 00:01:57,574 --> 00:02:00,779 And gives the player one attempt to guess the number. 32 00:02:05,697 --> 00:02:09,031 It's common to add a multi-line comment like this to the beginning of 33 00:02:09,031 --> 00:02:10,440 a JavaScript file. 34 00:02:10,440 --> 00:02:14,800 This is a place where many programmers put copyright notices and author information. 35 00:02:16,120 --> 00:02:20,704 Now, I'll add a single line comment that says when the game 36 00:02:20,704 --> 00:02:22,819 begins the guess is false. 37 00:02:27,011 --> 00:02:30,669 Developers usually include the comment directly above the code they're 38 00:02:30,669 --> 00:02:31,860 commenting on. 39 00:02:31,860 --> 00:02:35,370 In this case, I'm explaining what the next line of programming does. 40 00:02:35,370 --> 00:02:38,660 Now, you don't need to comment every line of your code, for example, 41 00:02:38,660 --> 00:02:40,900 the next two lines are pretty straightforward. 42 00:02:40,900 --> 00:02:43,730 This line stores a number and a variable, and 43 00:02:43,730 --> 00:02:49,430 the next line opens a prompt dialogue and stores the user's input in a variable. 44 00:02:49,430 --> 00:02:52,760 You also might add a comment that explains a piece of code. 45 00:02:52,760 --> 00:02:57,460 For example, the next part of this program is the first conditional statement. 46 00:02:57,460 --> 00:03:02,960 So you might add a multi-line comment to explain or outline what it does. 47 00:03:02,960 --> 00:03:07,060 First, test if a player's guess matches the number. 48 00:03:14,741 --> 00:03:18,162 Then change the value of correctGuess to true if they match. 49 00:03:28,623 --> 00:03:32,277 I'll add one last comment to explain what happens in the final conditional 50 00:03:32,277 --> 00:03:32,920 statement. 51 00:03:34,190 --> 00:03:37,634 Test if guess is correct and output response. 52 00:03:44,276 --> 00:03:49,080 Finally, there are a couple of other ways you might see comments used. 53 00:03:49,080 --> 00:03:52,130 For example, you can use single-line comments 54 00:03:52,130 --> 00:03:56,210 to annotate small specific snippets of your code, like this. 55 00:03:58,620 --> 00:04:01,900 These are also referred to as in line comments, 56 00:04:01,900 --> 00:04:04,930 since they're related only to the line they're written on. 57 00:04:04,930 --> 00:04:08,260 Now, too many of these comments can make your code harder to read, so 58 00:04:08,260 --> 00:04:09,660 you should use them sparingly. 59 00:04:10,850 --> 00:04:15,850 Finally, you can use comments to disable code and prevent it from running. 60 00:04:15,850 --> 00:04:18,930 For example, comment out a line of code like this. 61 00:04:20,090 --> 00:04:25,220 Now the JavaScript engine is not going to run this line because it's commented out. 62 00:04:25,220 --> 00:04:28,260 Using comments like this can be helpful when debugging, 63 00:04:28,260 --> 00:04:32,040 just make sure not to leave snippets of commented out code in your final script. 64 00:04:33,230 --> 00:04:37,348 So as you've learned, adding comments to your code is not only helpful for you, but 65 00:04:37,348 --> 00:04:40,248 also anyone else who needs to see your code in the future, so 66 00:04:40,248 --> 00:04:41,760 get in the habit of using them.