Locked lesson.
About this lesson
Understanding the instructions that are executed by the web browser, and learning how to comment code.
Exercise files
Download this lesson’s related exercise files.
Statements and Comments.docx58.8 KB Statements and Comments - Solution.docx
58.9 KB
Quick reference
Statements and Comments
Every line of code is a statement. Comments are important for code clarity.
When to use
You should always get in the habit of commenting your code. Always!
Instructions
The most basic way to create a comment is with two forward slashes //
Anything after the // is a comment, within that line.
You can also have multiple-lined comments.
/*
This is a comment
It's on more than one line
*/
Hints & tips
- Comments are important!
- Single line comments start with //
- Multiple line comments look like this /* comment */
- 00:05 Okay, in this video I wanna talk about statements and comments.
- 00:08 So I mentioned statements earlier and
- 00:10 I mentioned that they always end in semicolons.
- 00:13 So whenever you create some sort of program in JavaScript,
- 00:16 you'll usually use many statements.
- 00:18 Up until now we've been using these short little snippets of JavaScript to do things
- 00:23 and they've all been one short line of code or one statement.
- 00:26 For instance right here one statement, one line of code ends in a semi colon.
- 00:30 But normally you can expect a program to contain many statements.
- 00:34 And they all end in semi colons.
- 00:36 So for instance, just to give you an example here,
- 00:39 if we create a very simple program, and we call it var variable.
- 00:44 We haven't talked about variables.
- 00:45 We'll do that next.
- 00:46 But just kinda follow along here.
- 00:49 Here's a statement, and now we can define each of these,
- 00:52 say John b equals Elder,
- 00:58 c equals JavaScript.
- 01:05 I don't know, ha.
- 01:06 And then we go windows window.alert and then (a), let's say.
- 01:13 Now, this is just calling this a variable.
- 01:16 So, if we save this come back here to console.
- 01:20 Reload this page, boom there it says John right?
- 01:23 If we change this to (c) save it come back and hit reload, now it says JavaScript.
- 01:29 So this a very simple program.
- 01:31 It has 1, 2, 3, 4, 5 lines of code, 5 statements they all end in semicolons.
- 01:37 So the next thing I wanna talk about when it comes to statements is white space and
- 01:41 that is all of these spaces in here.
- 01:43 Now JavaScript for the most part ignores white spaces,
- 01:47 we just sort of use it to make our code more readable and
- 01:50 in fact we can remove all this white space like that and
- 01:55 like that and like that and if we save this come back and hit reload.
- 02:01 Here we get our output.
- 02:01 It says JavaScript.
- 02:03 The program runs exactly the same way that it did before,
- 02:06 there's just no white space.
- 02:08 Now, like I said you're always gonna wanna use white space as much as possible,
- 02:12 because it just makes everything more readable, right?
- 02:15 So white space is a big deal.
- 02:16 So those are a couple of syntax things.
- 02:18 Like I said, we're gonna talk about syntax more as we go along,
- 02:21 you're gonna pick up all these little quirks and rules.
- 02:23 So now I wanna talk about comments.
- 02:25 And comments are important no matter what programming language you use; you always
- 02:30 wanna comment your code, you want well-commented code.
- 02:33 It's just a convention, it's just what everyone wants to see.
- 02:35 For instance this program is very simple, if we come back a month from now and
- 02:40 we look at this code,
- 02:41 we're probably gonna be able to figure out what's going on here, right?
- 02:45 But as you create more complicated programs,
- 02:47 it's not always obvious what they're supposed to do.
- 02:50 And you might work on something for six months and then leave it alone for
- 02:52 a year and come back and you can't remember what's going on, like.
- 02:55 What was I doing there?
- 02:56 What was I trying to do?
- 02:57 Well that's what comments are great for.
- 02:59 You wanna comment your code.
- 03:00 In JavaScript to do a basic comment you just do
- 03:03 two forward slashes just like that.
- 03:04 Now everything after this is commented out.
- 03:07 So this is a comment, and you'll notice it's sort of grayed out.
- 03:11 That means that this won't get executed, this is not actual computer code.
- 03:15 This is just a comment and JavaScript will ignore this.
- 03:18 So in fact if we save it, come back here and
- 03:20 hit reload our program works exactly the same.
- 03:23 If we view our page source we can see there's the comment but
- 03:26 the program ignores it.
- 03:27 It doesn't do anything with it.
- 03:28 So that's very important.
- 03:30 That's one way to do comments.
- 03:31 You can also multiple line comments and that is just forward slash and
- 03:36 this, and now we can say this is a comment,
- 03:42 woohoo, to close this comment we just give it another star and a forward slash,
- 03:48 and you can see as soon as we do that everything else popped back into color and
- 03:52 this stuff remained, commented out, it's all gray.
- 03:56 So sometimes it's important to do multiple line comments and that's how you do it.
- 04:01 So that's comments, pretty simple.
- 04:03 Another way you can do comments on the same line as something, is just
- 04:07 the forward slash on the same line, you can do that, that's this is a comment.
- 04:13 Basically, everything after this slash is commented out, until the next line.
- 04:19 Then when the next line comes, its not commented.
- 04:21 So this is a very common way to comment code.
- 04:24 You want to do it on the same line, you just pop those in there.
- 04:27 So that's comments.
- 04:29 That is, those are statements and the next video we're gonna look at variables.
- 04:33 And that's all for this video.
Lesson notes are only available for subscribers.