How to write comments in ReactJS ?
Last Updated :
24 Apr, 2025
When working with ReactJS or any other programming language, making the code easy to understand is essential. One of the best ways to do this is by using comments. Comments are simple notes within the code that help to explain what is happening or why something was done in a specific way.
To write comments in ReactJS, we can use simple JavaScript comments as well as the JSX objects as per our requirement.
In ReactJS, there are two main ways to write comments:
To write comments in React for JavaScript code, like functions and classes, we will be using JavaScript comments. We can use single-line as well as multiline comments using JavaScript.
- Single-line comments: Use // for short comments on a single line.
- Multi-line comments: Use /* ... */ for longer explanations or to comment out multiple lines.
Now, let us understand with the help of the example:
JavaScript
import React, { Component } from 'react';
// This is a single line comment
class App extends Component {
/* This is a multiline comment*/
render() {
return (
<div>
<h1>Welcome to GeeksforGeeks</h1>
</div>
);
}
}
export default App;
Output:

2. Using JSX object
JSX is the syntax React uses to write HTML-like code inside JavaScript. When writing JSX, the way to add comments is a bit different. React requires comments to be placed inside curly braces {} and uses /* ... */ for multi-line comments.
Example: The above example does not work when we want to comment things inside the render block. This is because we use inside the render block and must use multi-line comment in curly braces {/* */}.
JavaScript
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>
// This is not a valid comment
/* Neither is this */
{/* THIS ONE IS A VALID COMMENT */}
<h1>Welcome to GeeksforGeeks</h1>
</div>
);
}
}
export default App;
Output

Note: We must remember, that even though JSX gets rendered just like normal HTML, it is actually a syntax extension to JavaScript. So, using <!-- --> as we did with HTML and XML will not work.
JavaScript
// Filename - App.js
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>
<!-- This is not a valid comment -->
{/* THIS ONE IS A VALID COMMENT */}
<h1>Welcome to GeeksforGeeks</h1>
</div>
);
}
}
export default App;
Output

- Comment Why, Not What: Focus on explaining why you're doing something, not just describing what the code is doing. This helps others understand the reasoning behind your approach.
- Avoid Redundant Comments: Don’t add comments for code that is self-explanatory. If the code is clear, a comment may not be necessary.
- Keep Comments Relevant: Only comment on complex or unclear sections of code. This helps avoid clutter and ensures comments are helpful.
- Update Comments Regularly: Always update comments when modifying the code. Outdated comments can mislead others and cause confusion.
Conclusion
In ReactJS, adding comments is important for making the code easier to understand. There are two main ways to add comments: using regular JavaScript comments for functions and logic, and using JSX comments inside curly braces for the render block.
Similar Reads
How to use events in ReactJS ? Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, m
2 min read
How to write ReactJS Code in Codepen.IO ? Now everything is online, some people use VScode to write react.js code and face most of the difficulty. The VScode requires setting for writing React.js code and Many beginners faced difficulty to use VScode so, for them, it is good and easy to use codepen. The codepen provide you with an online pl
2 min read
How to create components in ReactJS ? Components in React JS is are the core of building React applications. Components are the building blocks that contains UI elements and logic, making the development process easier and reusable. In this article we will see how we can create components in React JS. Table of Content React Functional C
3 min read
How to use Typography Component in ReactJS? Effectively present your design and content with clarity using typography. With Material UI for React, integration is a breeze. Utilize the Typography Component in ReactJS effortlessly by following this straightforward approach. Prerequisites:Node JS or NPMReact JSMaterial UISteps to create React Ap
2 min read
How to Create Typewriter Effect in ReactJS? Learn how to enhance your React JS applications with a captivating typewriter effect using CSS and JavaScript, leveraging the typewriter-effect package for seamless and engaging UI design.ApproachTo create a typewriter effect in React, we will be using the typewriter-effect npm package. Import and u
5 min read