Last Updated : 04 Apr, 2025
JSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, it’s actually a syntax extension for JavaScript.
What is JSX?JSX combines HTML and JavaScript in a single syntax, allowing you to create UI components in React. It simplifies rendering dynamic content by embedding JavaScript expressions inside HTML-like tags.
Syntax:
const element = <h1>Hello, world!</h1>;
When React processes this JSX code, it converts it into JavaScript using Babel. This JavaScript code then creates real HTML elements in the browser’s DOM . which is how your web page gets displayed.
JSX Transformation Processconst element = <h1>Hello, World!</h1>;
const element = React.createElement('h1', null, 'Hello, World!');
JSX can be implemented in a React project to create dynamic and interactive UI components. Here are the steps to use JSX in a React application:
npx create-react-app jsx-example
cd jsx-example
npm start
import React from "react";
function App() {
const message = "Hello, JSX works!";
return <h1>{message}</h1>;
}
export default App;
Output:
Here are some significant uses of JSX:
1. Embedding ExpressionsJSX allows you to embed JavaScript expressions directly within the HTML-like syntax. You can use curly braces {} to insert JavaScript expressions.
JavaScript
const name = 'Jonny';
const greeting = <h1>Hello, {name}!</h1>;
In this code
In JSX, attributes are specified similarly to HTML, but with some differences. Since JavaScript is used alongside JSX, certain attribute names are written in camelCase instead of the lowercase syntax used in HTML.
JavaScript
const element = <img src="" alt="A description" />;
In JSX, components or elements can accept children just like HTML elements. Children are nested elements or content that are passed into a component. This allows for flexible and reusable components.
JavaScript
const Welcome = (props) => {
return <div>{props.children}</div>;
};
const App = () => {
return (
<Welcome>
<h1>Hello, World!</h1>
<p>Welcome to React.</p>
</Welcome>
);
};
In this code
JSX is not directly rendered as HTML by React; instead, it gets compiled into JavaScript objects representing virtual DOM elements. These objects are later used by React to efficiently update the real DOM.
JavaScript
const element = React.createElement(
"button",
{
className: "btn",
onClick: () => alert("Clicked!"),
},
"Click Me"
);
The JSX code is converted into a JavaScript object
C++
{
type: 'button',
props: {
className: 'btn',
onClick: () => alert('Clicked!'),
children: ['Click Me']
}
}
In this code
JSX provides several advantages when working with React:
JSX is a powerful feature of React that allows you to write HTML-like code within JavaScript. It simplifies the process of building user interfaces by combining the flexibility of JavaScript with the structure of HTML. JSX makes it easier to render dynamic content, embed expressions, and manage event handling. React efficiently converts JSX into JavaScript objects, which are used to update the real DOM.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4