React JSX
React JSX
React JSX
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
React JSX
❮ Previous Next ❯
What is JSX?
JSX stands for JavaScript XML.
Coding JSX
JSX allows us to write HTML elements in JavaScript and place them in the DOM without
any createElement() and/or appendChild() methods.
You are not required to use JSX, but JSX makes it easier to write React applications.
Here are two examples. The first uses JSX and the second does not:
Example 1
https://www.w3schools.com/REACT/react_jsx.asp 1/11
4/2/25, 10:36 a.m. React JSX
JSX:
Tutorials Exercises Services Sign Up Log in
Run Example »
Example 2
Without JSX:
Run Example »
As you can see in the first example, JSX allows us to write HTML directly within the
JavaScript code.
JSX is an extension of the JavaScript language based on ES6, and is translated into
regular JavaScript at runtime.
Get Certified!
school
w3 s
5
CE
02
TI 2
Complete the React modules, do the exercises, take the exam and become
R
FI .
ED
w3schools certified!!
$95 ENROLL
https://www.w3schools.com/REACT/react_jsx.asp 2/11
4/2/25, 10:36 a.m. React JSX
The expression can be a React variable, or property, or any other valid JavaScript
expression. JSX will execute the expression and return the result:
Example
Execute the expression 5 + 5 :
Run Example »
Example
Create a list with three list items:
const myElement = (
<ul>
<li>Apples</li>
<li>Bananas</li>
<li>Cherries</li>
</ul>
);
Run Example »
https://www.w3schools.com/REACT/react_jsx.asp 3/11
4/2/25, 10:36 a.m. React JSX
So if you like to write two paragraphs, you must put them inside a parent element, like a
div element.
Example
Wrap two paragraphs inside one DIV element:
const myElement = (
<div>
<p>I am a paragraph.</p>
<p>I am a paragraph too.</p>
</div>
);
Run Example »
JSX will throw an error if the HTML is not correct, or if the HTML misses a parent
element.
Alternatively, you can use a "fragment" to wrap multiple lines. This will prevent
unnecessarily adding extra nodes to the DOM.
Example
Wrap two paragraphs inside a fragment:
const myElement = (
<>
https://www.w3schools.com/REACT/react_jsx.asp 4/11
4/2/25, 10:36 a.m. React JSX
<p>I am a paragraph.</p>
Tutorials
<p>I Exercisestoo.</p>
am a paragraph Services Sign Up Log in
</>
HTML
); CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Run Example »
Example
Close empty elements with />
Run Example »
https://www.w3schools.com/REACT/react_jsx.asp 5/11
4/2/25, 10:36 a.m. React JSX
JSX solved this by using className instead. When JSX is rendered, it translates
Tutorials Exercises Services
className attributes into class attributes.
Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Example
Use attribute className instead of class in JSX:
Run Example »
Conditions - if statements
React supports if statements, but not inside JSX.
To be able to use conditional statements in JSX, you should put the if statements
outside of the JSX, or you could use a ternary expression instead:
Option 1:
Example
Write "Hello" if x is less than 10, otherwise "Goodbye":
const x = 5;
let text = "Goodbye";
if (x < 10) {
text = "Hello";
}
https://www.w3schools.com/REACT/react_jsx.asp 6/11
4/2/25, 10:36 a.m. React JSX
Run Example »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Option 2:
Example
Write "Hello" if x is less than 10, otherwise "Goodbye":
const x = 5;
Run Example »
Note that in order to embed a JavaScript expression inside JSX, the JavaScript must be
wrapped with curly braces, {} .
❮ Previous Next ❯
https://www.w3schools.com/REACT/react_jsx.asp 7/11
4/2/25, 10:36 a.m. React JSX
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_jsx.asp 8/11
4/2/25, 10:36 a.m. React JSX
COLOR PICKER
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
ADVERTISEMENT
ADVERTISEMENT ADVERTISEMENT
https://www.w3schools.com/REACT/react_jsx.asp 9/11
4/2/25, 10:36 a.m. React JSX
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_jsx.asp 10/11
4/2/25, 10:36 a.m. React JSX
XML Examples C# Certificate
jQuery Examples
Tutorials Exercises Services XML Certificate
Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
https://www.w3schools.com/REACT/react_jsx.asp 11/11