JSX vs HTML
Understand the differences between HTML and JSX, including attributes, JavaScript expressions, event handlers, inline styles, and element structure.
1. What Is the Difference Between JSX and HTML?
HTML is a markup language used to structure web pages. JSX is a JavaScript syntax extension commonly used with React to describe user interfaces.
JSX looks similar to HTML, but some attribute names, syntax rules, and JavaScript integration features are different.
| Feature | HTML | JSX |
|---|---|---|
| CSS class attribute | class |
className |
| Label association | for |
htmlFor |
| JavaScript expressions | Require appropriate script context | Use curly braces in JSX |
| Event handler example | onclick |
onClick |
| Inline styles | CSS string | JavaScript object |
| Multiple sibling elements | Allowed in document structure | Group them when returning JSX from a component |
2. class vs className
In HTML, use class to apply CSS classes.
In JSX, use className.
HTML Example
<div class="student-card">
<h2>Samadhan</h2>
<p>Learning HTML</p>
</div>
Samadhan
Learning HTML
JSX Example
function App() {
return (
<div className="student-card">
<h2>Samadhan</h2>
<p>Learning React</p>
</div>
);
}
export default App;
Samadhan
Learning React
class, while JSX uses
className for CSS classes.
3. for vs htmlFor
HTML labels use the for attribute to associate
a label with a form field. JSX uses htmlFor.
function App() {
return (
<div>
<h2>Student Registration</h2>
<label htmlFor="studentName">
Student Name:
</label>
<input
id="studentName"
type="text"
defaultValue="Samiksha"
/>
</div>
);
}
export default App;
Expected Output
Student Registration
4. JavaScript Expressions in JSX
JSX allows JavaScript expressions inside curly braces. You can display variables, perform calculations, and generate dynamic content.
function App() {
const studentName = "Pradnya";
const subject1 = 85;
const subject2 = 90;
const total = subject1 + subject2;
return (
<div>
<h2>Student Result</h2>
<p>Name: {studentName}</p>
<p>Subject 1: {subject1}</p>
<p>Subject 2: {subject2}</p>
<p>Total Marks: {total}</p>
</div>
);
}
export default App;
Expected Output
Student Result
Name: Pradnya
Subject 1: 85
Subject 2: 90
Total Marks: 175
5. Event Handlers: HTML vs JSX
JSX uses camelCase event names such as
onClick and expects a function as the handler.
function App() {
function handleClick() {
alert("Welcome, Samadhan!");
}
return (
<div>
<h2>Event Handling
<button onClick={handleClick}>
Click Me
</button>
</div>
);
}
export default App;
Expected Output
Event Handling
{#if showWelcome}Welcome, Samadhan!
{/if}The preview button demonstrates the expected result. The React program above is the code students should write in their React application.
6. Inline Styles in HTML and JSX
HTML accepts CSS declarations as a string in the
style attribute. JSX uses a JavaScript object
with camelCase property names.
HTML
<h2 style="color: blue;
background-color: lightyellow;">
Samiksha
</h2>
JSX
function App() {
return (
<h2 style={{
color: "blue",
backgroundColor: "lightyellow"
}}>
Samiksha
</h2>
);
}
export default App;
Expected Output
Samiksha
7. Self-Closing Tags
In JSX, elements such as img and
input must be self-closed when written
without a closing tag.
function App() {
return (
<div>
<h2>Student Profile</h2>
<input type="text" placeholder="Enter name" />
<img src="/images/student.png"
alt="Student profile" />
</div>
);
}
export default App;
Expected Output
Student Profile
An image appears when a valid image asset is available at the specified path.
8. Conditional Rendering in JSX
JSX supports JavaScript conditional expressions such as the ternary operator. They can display different content based on a condition.
function App() {
const isCompleted = true;
return (
<div>
<h2>Course Status</h2>
<p>
{isCompleted
? "Samadhan completed the course."
: "Samadhan is still learning."}
</p>
</div>
);
}
export default App;
Expected Output
Course Status
Samadhan completed the course.
9. Common Mistakes
1. Using class instead of className
Incorrect JSX:
<div class="card">Hello</div>
Correct JSX:
<div className="card">Hello</div>
2. Forgetting to close an element
Incorrect JSX:
<input type="text">
Correct JSX:
<input type="text" />
3. Using an incorrect event handler
Incorrect JSX:
onclick={handleClick}
Correct JSX:
onClick={handleClick}
4. Forgetting keys in rendered lists
When rendering an array of elements, provide a stable
key such as student.id to each sibling
element returned by the list mapping.
10. Practice Questions
- Explain three differences between HTML and JSX.
- Write a JSX component that displays Samadhan's name using a JavaScript variable.
- Create a student registration form using
classNameandhtmlFor. - Create a button using the
onClickevent handler. - Display Samiksha's course completion status using a ternary expression.
- Render Samadhan, Samiksha, and Pradnya in a list using stable keys.
- Write a JSX heading with inline styles using a JavaScript object.
Summary
JSX is a JavaScript syntax extension used to describe React user interfaces. Unlike HTML, JSX uses className for CSS classes, htmlFor for label associations, curly braces for JavaScript expressions, camelCase event handlers, and JavaScript objects for inline styles. JSX elements must follow valid syntax, and mapped list items should have stable keys. Understanding these differences helps you write clean and maintainable React applications.