What is JSX and Why Is It Used in React?
π‘ Concept Name
JSX (JavaScript XML) is a syntax extension that allows writing HTML elements directly within JavaScript. It is used in React to describe what the UI should look like.
π Quick Intro
JSX makes it easier to visualize component structure by embedding HTML-like code within JavaScript. Though browsers canβt read JSX directly, tools like Babel compile it into plain JavaScript (React.createElement calls) that the browser can understand.
π§ Analogy / Short Story
Imagine writing a recipe where you mix the list of ingredients and cooking steps side-by-side for each dish. JSX does something similarβit mixes HTML structure with JavaScript logic in one place, making the UI definition clean and cohesive.
π§ Technical Explanation
- π€ JSX stands for JavaScript XMLβa syntax extension used with React.
- βοΈ JSX gets transpiled by Babel into
React.createElement()
calls. - π¦ JSX allows embedding expressions using curly braces
{ }
. - π JSX improves code readability by colocating markup with logic.
- π JSX must have one parent element and use
className
instead ofclass
.
π» Real Code Example
// JSX Example
const name = "React";
const element = <h1>Hello, {name}!</h1>;
ReactDOM.render(element, document.getElementById('root'));

β Interview Q&A
Q1: What is JSX in React?
A: JSX is a syntax extension that allows mixing HTML-like tags in JavaScript, making UI code more readable.
Q2: Why do we use JSX in React?
A: JSX simplifies UI structure definition and enhances code readability by colocating markup and logic.
Q3: Can browsers understand JSX directly?
A: No, JSX is transpiled into JavaScript using tools like Babel before it runs in the browser.
Q4: How does JSX improve productivity?
A: JSX reduces context switching between HTML and JS, making code easier to write and maintain.
Q5: What does JSX compile into?
A: It compiles into calls to React.createElement()
, which builds the virtual DOM.
Q6: Is JSX mandatory in React?
A: No, but itβs highly recommended due to improved readability and convenience.
Q7: What syntax rules must JSX follow?
A: JSX must have one parent tag, use className
instead of class
, and close all tags.
Q8: Can we write JavaScript expressions in JSX?
A: Yes, by wrapping them inside curly braces { }
.
Q9: What tool is commonly used to compile JSX?
A: Babel is used to convert JSX into standard JavaScript.
Q10: Whatβs the difference between HTML and JSX?
A: JSX looks like HTML but has different rules (e.g., className
, self-closing tags) and supports embedding JavaScript.
π MCQs
Q1. What does JSX stand for?
- JavaScript Extension
- JavaScript eXample
- JavaScript XML
- Java Static Exchange
Q2. Why is JSX used in React?
- To define routes
- To manage state
- To write HTML-like syntax in JavaScript
- To handle databases
Q3. Which tool compiles JSX to JavaScript?
- Webpack
- Gulp
- Grunt
- Babel
Q4. What does JSX compile into?
- Java bytecode
- HTML
- React.createElement() calls
- DOM tree
Q5. What tag attribute must be used in JSX instead of 'class'?
- css
- class
- style
- className
Q6. Is JSX a requirement for writing React apps?
- Yes
- Only in React Native
- No, but it's recommended
- Only for server-side apps
Q7. How do you insert JavaScript in JSX?
- With square brackets []
- Inside quotes
- Using curly braces {}
- With angle brackets <>
Q8. What is a limitation of JSX syntax?
- No expressions allowed
- Cannot use variables
- Must use inline styles
- Must have one parent element
Q9. Which of the following is valid JSX?
- div Hello /div
- <div>Hello</div>
- <div Hello>
- <Hello>div</Hello>
Q10. How does JSX improve React development?
- Slows down app
- Removes components
- Improves readability and UI logic colocation
- Handles routing
π‘ Bonus Insight
JSX bridges the gap between design and logic. It simplifies UI code by combining structure and behavior, which is why modern development environments favor it over separating HTML and JavaScript entirely.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!