Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to comment out in ReactJS

Understanding the Basics

Before we delve into the nitty-gritty of how to comment out in ReactJS, let's take a step back and look at the bigger picture. Imagine you're reading a book, and the author has left little notes in the margins explaining certain words or concepts. These notes are not part of the story but they help you understand it better. Similarly, in programming, we use comments to explain what a piece of code does. These comments are not executed as part of the code, but they help other programmers (or even your future self) understand it.

Importance of Comments

In the world of programming, we often liken code to a story. Just like how a good story is easy to follow, good code should also be easy to understand. One way to make your code more readable is by using comments. These are little notes that you can sprinkle throughout your code to explain what's happening. They're like breadcrumbs leading the way through a dense forest of code.

Single-line Comments

Imagine you're writing a shopping list. You've put down 'milk', but you want to remind yourself to check if there's any left in the fridge before buying more. You could write this reminder as a little note next to 'milk' on the list. In ReactJS, this is similar to a single-line comment. Here's how you would write a single-line comment in ReactJS:

// This is a single-line comment

The // tells ReactJS that everything following it on the same line is a comment, not code to be executed.

Multi-line Comments

Now, let's say you have a lot to say about 'milk'. Maybe you want to remind yourself of different brands to consider, or note down the price range. Your note is going to spread over multiple lines. In ReactJS, this is a multi-line comment. Here's how you write a multi-line comment:

/* This is a multi-line comment
 and it spans over multiple lines
 until it's closed with */

The /* tells ReactJS that everything following it until the */ is a comment. It doesn't matter how many lines are in between, all of it will be treated as a comment.

Comments in JSX

JSX is a syntax extension for JavaScript. It's not necessary to use it when writing ReactJS, but it makes the code easier to read and write. It's kind of like choosing to write a letter by hand instead of typing it. It's not necessary, but some people find it easier and more enjoyable.

In JSX, you can't use the JavaScript comment syntax directly. Instead, you wrap the comment in curly braces {}. Here's how you would do it:

  // This is a single-line comment in JSX

  /* This is a multi-line comment
     in JSX */

Inline Comments in JSX

Sometimes, you want to add a comment right in the middle of your code, rather than on a new line. In ReactJS, this is called an inline comment.

  Hello, {/* This is an inline comment */} world!

In the above code, 'Hello, world!' would be displayed, but the comment would be ignored by ReactJS.


Learning how to comment out in ReactJS is like learning the art of leaving helpful notes for yourself and others. It's about making your code clear and understandable, not just to machines, but also to humans. After all, programming is as much about communicating with people as it is about instructing machines.

So, the next time you find yourself lost in a dense forest of code, remember to drop breadcrumbs in the form of comments. They might just lead the way for a fellow programmer, or even your future self. Happy coding!