What are the advantages of using JSX in ReactJS ?
Last Updated : 10 Nov, 2023
JavaScript XML or JSX is an extension of the JavaScript language syntax. The React library is an extension to write XML-like code for elements and components. JSX tags have a tag name, attributes, and children.
Although JSX is not a necessity to write React applications, it is extremely beneficial as it makes React code simpler and elegant.
Syntax:
{/* Using JSX*/}
<div className="App">GeeksforGeeks</div>;// Without JSX
React.createElement(
"div",
{ className: "App" },
"GeeksforGeeks"
);
Advantages of using JSX in React JS :
- JSX helps us keep our code simpler and elegant when writing large pieces of code.
- According to the React docs, most people find it helpful as a visual aid when working with UI inside the JavaScript code.
- JSX also allows React to show more useful error and warning messages.
- If one is familiar with HTML, it is quite easy to use JSX when building React application
- Faster than normal JavaScript as it performs optimizations while translating to regular JavaScript.
Steps to Create React Application:
Step 1: Create a React application using the following command:
npx create-react-app example
Step 2: After creating your project folder i.e. example, move to it using the following command:
cd example
Project Structure:

Example: This example implements two same components using one with JSX and other without JSX.
JavaScript // Filename - App.js import React from "react"; import Jsx from "./Components/Jsx"; import Nojsx from "./Components/Nojsx"; export default function App() { return ( <div className="App"> <Jsx /> <Nojsx /> </div> ); }
JavaScript // Filename - Components/Nojsx.js import React from "react"; const Nojsx = () => { return React.createElement( "div", { id: "Nojsx", className: "GfgClass" }, React.createElement("h1", null, "Welcome to GFG") ); }; export default Nojsx;
JavaScript // Filename - Components/Jsx.js import React from 'react' const Jsx = () => { return ( <div className ='GfgClass'> <h1>Welcome to GFG</h1> </div> ) } export default Jsx
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Output Similar Reads
What are the advantages of React.js ? React.js is a popular JavaScript library used for building dynamic and interactive user interfaces. It has become a go-to tool for developers due to its efficient architecture, high performance, and ease of use. This article highlights the key advantages of using React.js for web development and exp
5 min read
What are the advantages of using Redux with ReactJS ? Redux is a state management tool for JavaScript applications. It is more commonly used with ReactJS but is also compatible with many other frameworks such as Angular, Vue, Preact, as well as vanilla JavaScript. It is important to note that even though React and Redux are frequently used together, th
3 min read
What are the features of ReactJS ? Created by Facebook, ReactJS is a JavaScript library designed for crafting dynamic and interactive applications, elevating UI/UX for web and mobile platforms. Operating as an open-source, component-based front-end library, React is dedicated to UI design and streamlines code debugging by employing a
4 min read
Advantages of using Functional Components with React Hooks In the past, React mainly used class-based components as its building blocks. However, functional components, which are essentially JavaScript functions that receive props (inputs) and return JSX (rendered content), were limited in their capabilities. With React 16.8 and the introduction of hooks, f
4 min read
What is the use of data-reactid attribute in HTML ? The data-reactid attribute is a custom attribute that react can easily identify its components within the DOM. Just like the HTML "classes" and "id" attributes, "data-reactid" helps in uniquely identifying the element.What is data-reactid?The data-reactid is an attribute that is used as a unique ide
2 min read