02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Commenting out Code we don't want to render

There are a few ways to comment out script. We will see how to comment out our HTML and how to comment out JavaScript as well.

Video Transcript

00:00All right, so the next topic we want to talk about was commenting.
00:03How do you comment something out of HTML in general?
00:08We've been doing HTML for a while.
00:09We didn't really learn about how to comment things out.
00:12It's time for us to do that.
00:13So commenting is incredibly simple.
00:15What you want to do is you start off with the smaller than.
00:19And then you write the...
00:22And then minus, minus.
00:25Once you do that, notice how everything dimmed out because I have a smart editor.
00:29And then when you're done with your comment, you will close it up using minus, minus and
00:34then greater than.
00:36And now everything in between here will be invisible to the user.
00:39So let's see that in a working example.
00:41So let's say for example we want to comment out our script tag.
00:45So I could basically start it off in our script tag and I could end it up basically making
00:49sure that our script tag will be completely invisible to users and they won't be able
00:53to see it.
00:54Now this is only true for HTML.
00:57It's not a commenting structure for JavaScript.
01:00Now notice that I'm saying that because it's important why I'm saying that and we're going
01:03to talk about that in a second.
01:05All right.
01:06So now when we look into our browser now and we click on refresh, you'll see that we're
01:08not going to get that alert message anymore because our JavaScript has been disabled.
01:13Now again, the reason why I said that notice that is because we put our script outside,
01:18we put our code outside of the JavaScript area.
01:21So we basically canceled the JavaScript from ever being created.
01:25Contrary to that, if I undo this and I put my commenting right inside of my JavaScript
01:31window and I comment out and I comment out all the way here, it's not commenting out
01:37my JavaScript.
01:38The reason is it's only going to comment out actually HTML.
01:42Now the reason why this is done and a lot of programmers will do this especially in
01:46older websites is because older websites did not know how to deal with scripting.
01:52And because they didn't know how to deal with scripting, they would just take the
01:55copy itself and output it on the user's screen.
01:59In this day and age, it's so uncommon to find a browser that has that issue.
02:03So you don't see it as often that people put these commenting in.
02:06Although if you put your JavaScript in an external file, you don't have to worry about
02:10it at all.
02:11So it's another plus of working with external files, which is something that we're going
02:14to look at immediately following this video.
02:16All right.
02:17So now you know how to do commenting and you don't, you could comment not only JavaScript,
02:21you could also comment let's say our title, our H1 title here.
02:24So again, I made sure that I put my commenting and I close my commenting, I save it and there
02:30you go.
02:31Our JavaScript will still work because it doesn't get commented out.
02:33This is only a hack to kind of make sure that old browsers don't render the JavaScript
02:38as if it's just text because this is a programming code, it's not text, it should be displayed.
02:44But if we go and we refresh our screen, we should have an empty screen, we should
02:47have null, I might have not saved it.
02:50So let me just go back to my browser here, I might have not saved it because we're still
02:55saying that H1 and we shouldn't be seeing the H1.
02:58So I'm going to click there and refresh again and here we go.
03:00Now we don't see that H1 anymore.
03:02All right.
03:03So that's about commenting, that's all we have to say about commenting.
03:05In the next video, we're going to talk about how do you load in JavaScript files and
03:09beyond that, how do you tell the browser when should it load it and we're going to
03:13see that immediately in the following video.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!