How to Write Comments in JSX (React)
In React, JSX allows you to write HTML-like syntax inside JavaScript. However, writing comments inside JSX is a little different compared to regular JavaScript.
Commenting Inside JSX Elements
When writing comments inside JSX (within the return statement), you must wrap your comment with {}
and use the standard JavaScript comment syntax /* comment */
inside it.
Example:
function ExampleComponent() { return ( <div> {/* This is a comment inside JSX */} <h1>Hello, World!</h1> </div> ); }
✅ Important points:
- Curly braces
{}
tell JSX to treat what's inside as JavaScript. - Inside the braces, you use
/* */
for multiline-style comments.
Commenting Outside JSX
When you're outside the JSX (in the JavaScript part of your code), you use normal JavaScript comments without any extra syntax.
Example:
// This is a normal JavaScript comment function ExampleComponent() { return ( <div> <h1>Hello, again!</h1> </div> ); }
Quick Tips
- Never use
//
comments directly inside JSX — it will cause an error. - Always use
{ /* your comment */ }
inside JSX blocks.