Javascript Comments

JavaScript Comments

  1. Explain comments in programming languages
  2. Use the two kinds of JavaScript comments

What is a comment?

When programming, it's often useful to be able to document what we're thinking — maybe for someone else who might read the code, or just for ourselves later. We don't necessarily want this documentation to run or influence the code in any way, but we want to add some notes right next to the code for safe keeping. This is where comments come in.

Why comment?

Not only do comments allow us to express the intent of our program in natural language (which is, naturally, easier for ourselves and others to reason about), allowing us to communicate the intent of our program to people who have never seen it before; comments also help us to identify subtle misunderstandings before they become bugs.

Think of it this way: often, we feel that we have understood a problem and implemented a good solution. Say we're making a cake, and we want it to be fluffier — we've heard of fluffy omelets, so we think that adding an extra egg or two to the cake might be the solution we're looking for.

But then when we say this aloud — outside of the realm of the recipe and the baking process — we realize that blindly adding eggs to a cake might not achieve the desired result. Our cake's lack of fluffiness is due to something else.

Comments work similarly. We might assume that a program works in a certain way, but when we describe how it works aloud (or in comments), we realize that we've misunderstood something fundamental — and now we can correct our misunderstanding before it turns into a bug.

Also, you'll see comments throughout the lessons on Learn — we want to make sure that you're familiar with them.

Comments in JavaScript

JavaScript supports two ways of demarcating comments: // and /**/.

Single Line Syntax: //

// makes everything following it on the same line into a comment.

Enter the following in console (remember how to open it up? In Chrome: cmd + option + j on a Mac, or ctrl + shift + i for Windows):

// I'm a comment!

Hmm... nothing happened.


Comments are how we document our code. They might seem boring, and you might wonder why we're not learning about "real" coding — but comments are real code. The most difficult thing about reading code is understanding it — comments give us ways of communicating the intent of our code to anyone who might read it (including ourselves after some time away from it), and so they serve a vital function for helping others understand our code.

What happens if we make a mistake when entering a comment? Say we forget the //:

But I'm no longer a comment (and will cause an error)!

We get an error:


Top Tip: Errors aren't something to be afraid of. They look scary with their boisterous red type and aggressive names, but they're here to help us. We'll learn more about errors and how to use them as we go through the course.

// is useful for short notes. It's good practice to give a comment its own line, even though the following is perfectly valid:

1 + 2 // nothing before the `//` is a comment, so it will run; but this comment is just fine -- by the way, the answer is 3

Comment Block Syntax: /**/

You might notice a beautiful symmetry with this comment mark — and indeed, it splits nicely in two. The first half, /* tells us where the comment block starts; the second half */ tells us that it's ended.

Enter the following in console (you'll need to use shift + enter to enter a newline instead of evaluating the code):

 I'm a comment, because I'm inside `/*`.
 Still a comment.
 Let's end this comment at the end of this line */

 No longer a comment!

As you can see, we can do whatever we want between /* and */. This kind of multi-line comment is useful for longer notes, documentation, etc.

Note that you can still use /**/ on just one line:

/* I'm a comment */

I'm not a comment

In this case, it's up to you whether /**/ or // makes more sense.

View JavaScript Comments on and start learning to code for free.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us