React What is JSX in React? Features, Advantages, Working & Example
Introduction to JSX in React
React.js is a popular JavaScript library used to create
interactive and dynamic user interfaces. When developers build applications
using React, they often need to display HTML-like content inside JavaScript
code. This is where JSX becomes useful.
JSX stands for JavaScript XML. It is a syntax extension
for JavaScript that allows developers to write HTML-like elements directly
inside JavaScript code. JSX makes React applications easier to understand,
write, and maintain.
For example, instead of creating HTML elements using
JavaScript functions, developers can write familiar tags such as <h1>,
<p>, and <div> inside a React component.
2. What is JSX in React?
JSX stands for JavaScript XML. It is a JavaScript syntax extension that allows developers to write HTML-like code inside JavaScript. React uses JSX to describe how the user interface should look.
JSX looks similar to HTML, but it follows some special
rules. For example, developers must use className instead of class when adding
CSS classes, and JSX elements must be properly closed.
Here is a simple example of JSX:
function App() {
return (
<div>
<h1>Welcome to React</h1>
<p>Learn JSX step by step.</p>
</div>
);
}
In this example, the App() function is a React component.
It returns JSX containing a heading and a paragraph.
One important feature of JSX is that it allows JavaScript
expressions to be written inside curly braces {}. This helps developers display
dynamic information.
For example:
const studentName = "Aishwarya";
return
<h1>Hello, {studentName}!</h1>;
}
The output will display the student's name inside the
heading.
JSX is converted into JavaScript by tools such as Babel
or the JSX transform used by modern development tools. React then uses the
resulting JavaScript to create and update the user interface.
3. History of JSX
JSX was introduced by the React development team at
Facebook (now Meta) as part of the React ecosystem. React was first released
publicly in 2013, and JSX became an important part of the way developers wrote
React components.
Before JSX became popular, developers created user
interface elements using JavaScript functions such as React.createElement().
Although this method worked, it could become difficult to read when building
complex interfaces.
JSX provided a more readable alternative by allowing
developers to write HTML-like syntax inside JavaScript. Today, JSX is widely
used with React and supported by modern tools such as Vite, Babel, and other
JavaScript build tools.
4. Features of JSX in React
JSX provides several useful features that make React
development easier. The following are some important features of JSX.
1. HTML-Like Syntax
JSX allows developers to write HTML-like elements inside
JavaScript. It makes the user interface structure easier to understand.
<h1>Welcome to My Website</h1>
2. JavaScript Expressions
Developers can insert JavaScript expressions inside JSX
using curly braces {}.
const age = 22;
return
<h2>My age is {age}</h2>;
}
3. Component-Based Structure
JSX works naturally with React components. Developers can
create reusable components and use them inside other components.
function Header() {
return
<h1>My React Website</h1>;
}
return <Header
/>;
}
4. Supports Conditional Rendering
JSX allows developers to display different content
depending on a condition.
const isLoggedIn = true;
return
<h2>{isLoggedIn ? "Welcome Back!" : "Please
Login"}</h2>;
}
5. Easy to Maintain
JSX keeps the user interface structure close to the
JavaScript logic that controls it. This can make React applications easier to
read and maintain.
6. Supports Dynamic Data
Developers can display values from variables,
calculations, and JavaScript expressions directly inside JSX.
These features make JSX a useful tool for creating modern
React applications.
5. Advantages and Disadvantages of JSX
JSX has many benefits, but beginners should also
understand its limitations.
Advantages of JSX
- Easy
to Understand: Its HTML-like syntax is familiar to web developers.
- Readable
Code: JSX makes the structure of user interfaces easier to understand.
- JavaScript
Support: Developers can use JavaScript expressions inside JSX.
- Reusable
Components: JSX works well with React's component-based architecture.
- Error
Detection: Development tools can identify many syntax errors during
compilation.
Disadvantages of JSX
- Learning
Curve: Beginners must understand JSX rules and JavaScript expressions.
- Build
Tools Required: JSX generally needs a compiler or build tool before
browsers can execute it.
- Different
Syntax Rules: Developers must learn rules such as using className instead
of class.
- Complex
Expressions: Large amounts of JavaScript logic inside JSX can make
components difficult to read.
6. How Does JSX Work in React?
JSX works by allowing developers to write HTML-like
syntax inside JavaScript files. However, web browsers do not directly
understand JSX syntax. Therefore, JSX must first be transformed into regular
JavaScript.
Let's understand the process step by step.
Step 1: Write JSX Code
First, developers write JSX inside a React component.
function App() {
return
<h1>Hello React!</h1>;
}
Step 2: JSX Transformation
A tool such as Babel or the JSX transform configured by
Vite converts JSX into JavaScript instructions.
For example, the JSX code can be transformed conceptually
into:
import { jsx } from "react/jsx-runtime";
return
jsx("h1", {
children:
"Hello React!"
});
}
Step 3: React Creates the User Interface
React uses the resulting JavaScript to describe the
elements that should appear on the page.
Step 4: Display Output in the Browser
React renders the user interface into the browser's DOM
and updates the necessary parts when data or state changes.
In simple words: Developers write JSX, a build tool
transforms it into JavaScript, and React uses that JavaScript to display the
user interface in the browser.
7. JSX Code Examples in React with Output
Let's understand JSX with practical examples. These
examples are useful for students who are learning React for the first time.
Example 1: Simple JSX Heading
App.jsx
function App() {
return (
<div>
<h1>Welcome to Web Designing Theory</h1>
<p>Learn React with practical examples.</p>
</div>
);
}
Output:
Welcome to Web Designing Theory
Learn React with practical examples.
Explanation: This example displays a heading and
paragraph using JSX inside a React component.
Example 2: JSX with JavaScript Variables
App.jsx
function App() {
const name =
"Aishwarya";
const course =
"React JS";
<div>
<h2>Hello, {name}!</h2>
<p>You
are learning {course}.</p>
</div>
);
}
Output:
Hello, Aishwarya!
You are learning React JS.
Explanation: Curly braces {} allow developers to display
JavaScript variable values inside JSX.
Example 3: JSX with a Button
App.jsx
function App() {
function
showMessage() {
alert("Welcome to React!");
}
<div>
<h2>React Event Example</h2>
<button
onClick={showMessage}>
Click Here
</button>
</div>
);
}
Output:
React Event Example
Click Here
Interactive preview of the example. In the actual React
application, the code displays a browser alert.
Explanation: The onClick event handles button clicks.
When the user clicks the button, the showMessage() function runs and displays
an alert message.
Example 4: JSX with Conditional Rendering
App.jsx
function App() {
const isLoggedIn
= true;
<div>
<h2>
{isLoggedIn
?
"Welcome Back!"
:
"Please Login"}
</h2>
</div>
);
}
Output:
Welcome Back!
8. Frequently Asked Questions About JSX
Q1. What is JSX in React?
JSX stands for JavaScript XML. It is a syntax extension
that allows developers to write HTML-like elements inside JavaScript code.
Q2. Is JSX compulsory in React?
No. JSX is not compulsory in React. Developers can also
create React elements using JavaScript functions such as React.createElement().
Q3. What is the full form of JSX?
The full form of JSX is JavaScript XML.
Q4. Why do we use JSX in React?
JSX makes React code easier to read and write. It allows
developers to combine JavaScript expressions with HTML-like user interface
elements.
Q5. Can we write JavaScript inside JSX?
Yes. JavaScript expressions can be written inside curly
braces {} in JSX.
Q6. What is the difference between HTML and JSX?
HTML is a markup language used to structure web pages,
whereas JSX is a JavaScript syntax extension used to describe user interfaces
in React. JSX also follows special rules such as className and camelCase event
names.
Q7. Does the browser understand JSX directly?
No. Browsers do not understand JSX syntax directly. It
must be transformed into JavaScript before execution.
Q8. Which file extension is used for JSX?
The .jsx extension is commonly used for React components
containing JSX. However, JSX can also be written in .js files when the project
configuration supports it.
Q9. What are curly braces {} used for in JSX?
Curly braces are used to insert JavaScript expressions,
such as variables, calculations, and function results, into JSX.
Q10. Which tools support JSX?
Tools such as Vite and Babel support JSX transformation
and help developers build React applications.
8. Conclusion
JSX is an important part of modern React development. It
allows developers to write HTML-like syntax inside JavaScript, making user
interface development easier and more readable. In this tutorial, we learned
the meaning of JSX, its history, features, advantages, disadvantages, working
process, and practical code examples with output.

Comments
Post a Comment