What Is JSX?
Understand JSX, why React developers use it, how it works with JavaScript, and how to write JSX in a React component.
1. What Is JSX?
JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows developers to describe user interfaces using HTML-like markup inside JavaScript code.
React developers commonly write JSX inside components. It makes UI code easier to read because the structure of the interface appears close to the JavaScript logic that defines it.
2. Why Do We Use JSX?
Without JSX, UI elements can be created using
createElement(). For complex interfaces,
that approach can become difficult to read.
JSX provides a clearer way to organize UI elements.
Readable Code
The component's markup is easy to read and understand.
Clear Structure
Parent and child elements are visible in the code.
JavaScript Expressions
Variables and calculations can be inserted into JSX.
Component-Based UI
JSX can be returned from reusable React components.
3. How JSX Connects with React
JSX describes the interface, JavaScript provides the logic, and React uses the resulting element descriptions to render the user interface.
JavaScript Function
A component returns JSX.
JSX
JavaScript
JSX is transformed into executable JavaScript.
Browser UI
The user sees Hello React!
4. First JSX Example
The following React component returns a heading written in JSX.
function App() {
return <h1>Hello React!</h1>;
}
export default App;
Code Explanation
function App()
Defines a React function component.
return
Returns the UI description from the component.
<h1>Hello React!</h1>
JSX describing the heading to display.
export default App
Allows other files to import this component.
Expected Browser Output
Hello React!
5. JSX Looks Similar to HTML
JSX uses tags similar to HTML tags. You can use headings, paragraphs, lists, buttons, and other elements to describe your UI.
function App() {
return (
<div>
<h1>CIIT Training Institute</h1>
<p>Learn software development.</p>
<ul>
<li>Java Full Stack</li>
<li>Python Full Stack</li>
<li>React Development</li>
</ul>
</div>
);
}
export default App;
Expected Output
CIIT Training Institute
Learn software development.
- Java Full Stack
- Python Full Stack
- React Development
className, and elements must be properly closed.
6. JSX Can Use JavaScript Expressions
Curly braces { } let you place JavaScript
expressions inside JSX. This is useful when you want to
display data stored in variables or calculate a value.
function App() {
const studentName = 'Priya';
const courseName = 'React Development';
const totalMonths = 5;
return (
<div>
<h1>Welcome, {studentName}!</h1>
<p>Course: {courseName}</p>
<p>Duration: {totalMonths} months</p>
<p>Remaining example value: {totalMonths - 2}</p>
</div>
);
}
export default App;
Expected Output
Welcome, Priya!
Course: React Development
Duration: 5 months
Remaining example value: 3
{totalMonths - 2} produces
the number 3.
7. JSX vs HTML
Although JSX looks like HTML, it is used inside JavaScript and has some different syntax rules.
| Feature | HTML | JSX |
|---|---|---|
| CSS classes | class="card" |
className="card" |
| Click event | onclick |
onClick |
| JavaScript variables | Not inserted into regular HTML text using JSX syntax | {'{' }variable{' }'} |
| Inline styles | Normally use a CSS string | Use a JavaScript style object |
| Self-closing element | Some HTML elements do not require a closing tag | Write self-closing JSX elements with a slash, such as <img /> |
| Component output | HTML forms part of a document | A component returns JSX or another supported React node |
8. JSX and JavaScript Relationship
JSX is written as part of JavaScript source code. You can define variables and functions, then use their values within JSX to create dynamic interfaces.
function App() {
const courseCount = 3;
function getWelcomeMessage() {
return 'Welcome to React learning!';
}
return (
<div>
<h1>{getWelcomeMessage()}</h1>
<p>Available courses: {courseCount}</p>
</div>
);
}
export default App;
Expected Output
Welcome to React learning!
Available courses: 3
if statement cannot be placed directly inside
JSX expression braces; use appropriate JavaScript logic
outside the returned markup or conditional expressions instead.
9. Common JSX Mistakes
| Mistake | Correct Approach |
|---|---|
Using class in JSX |
Use className for CSS classes. |
| Missing closing tags | Close each element or use self-closing syntax. |
| Returning adjacent root elements without grouping | Wrap them in one parent or a React Fragment. |
| Writing a variable name as ordinary JSX text | Use curly braces to evaluate a variable. |
| Using lowercase for a custom component | Use an uppercase component name, such as CourseCard. |
10. Practice Task
Task: Create a Student Information Component
Create an App component that displays
information about a student using JSX.
- Create a variable called
studentName. - Create a variable called
courseName. - Create a variable called
duration. - Display all three values using curly braces.
- Add a heading and a paragraph for each value.
- Run the application and verify the browser output.
Summary
- JSX stands for JavaScript XML.
- It is a syntax extension for describing user interfaces.
- JSX can be returned from React components.
- JavaScript expressions can be used inside JSX curly braces.
- JSX looks similar to HTML but follows different rules.
- Development tooling transforms JSX into JavaScript.
- Proper JSX syntax makes React code easier to maintain.