How to write comments in ReactJS ? (original) (raw)

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.

**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 (

Welcome to GeeksforGeeks

); } } 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 (

// This is not a valid comment /* Neither is this / {/ THIS ONE IS A VALID COMMENT */}

Welcome to GeeksforGeeks

); } } 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 (

            {/* THIS ONE IS A VALID COMMENT */}
            
            <h1>Welcome to GeeksforGeeks</h1>
        </div>
    );
}

}

export default App;

`

**Output

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.