[MUSIC].
>> Code comments. Hey everybody.
My name is Burke Holland.
In this video we're going to take a look at
code comments in JavaScript.
Code comments are lines in
your JavaScript code that the JavaScript runtime just ignores.
Now, you might want to do this for several reasons.
Could be to leave a handy tip in
the code that explains and give some context.
Could be to make a reminder for yourself
when you're developing for something you
need to come back and complete later.
Or it could be to just take an active line or block of lines of
code and comment them out during
development so that they don't actually execute,
but you're not deleting them from the file.
Now when it comes to comments in JavaScript,
there are two types.
There's single-line comments and multi-line comments.
Single-line comments are indicated
by two slashes at the beginning of a line.
Now usually the editor that you're using will gray these
lines outer change their colors
so that you know that they're commented out.
A single-line comments only apply to the current line,
so any code on subsequent lines
still executes and is not commented out.
Multi-line comments are designed to comment out
multiple lines without having to put
those slashes at the beginning of every line.
Multi-line comments start with a slash and then a star,
and they end in reverse with a star and then a slash.
You can think of multi-line comments as
starting with a slash and ending with a slash.
Everything that's in between that is
commented out no matter how many lines it takes up.
Now, in the real world,
most editors have a keyboard shortcut just
for commenting lines outs and then removing the comments.
For VS Code, that's comment for
Mac or Ctrl for Windows and the L key.
This just toggles comments on and off for a line.
Normally instead of using multi-line comments,
we'll just highlight a block of code and then use Command or
Ctrl+L to toggle the comments on or toggle them off.
You don't use multi-line comments as much as you think that you
might encode because the editor handles the single line comments.
We don't really care how many double slashes they
are since we're not manually putting them in or taking them out.
Now, it's as important to talk about
when you should use comments as it is about what comments are.
When you first start programming,
you're going to feel like you need to comment
everything and say what everything does.
I did this, I had comments on all of my code.
That's actually unnecessary and
ironically makes your code harder to read.
If your code can't be understood by someone who knows JavaScript,
then instead of a comment,
what you might need is to refactor
the code to make it simpler and easier to understand.
Now, oftentimes what this means is
taking a chunk of code that has a comment that explains
what it does and wrapping it in
a function that has a name that describes what the code does.
For instance, we might have a block
of code that reverses the string and
a comment that lets the reader know this code reverses the string.
A better solution would be to take that code,
put it in a function called reverse string and
now the code is self-documenting.
We don't need a comment here,
the person reading the code
can look at it and discern from the function name,
oh, this code reverses a string.
In summary, there are two types of comments in JavaScript.
There are single-line comments that
use two slashes and only apply to the current line.
There are multi-line comments that start
with a slash and a star and end with
a star and a slash and everything in between
that is commented out no matter how many lines you use.
Lastly remember, take it easy on comments,
you probably don't need as many as you think you do.
Use them judiciously, see if you can make
your code self-documenting instead.