JSX (JavaScript XML) lets you write HTML-like syntax directly inside JavaScript. It is not plain HTML — it is syntactic sugar for JavaScript function calls that return React elements. JSX makes your UI code more readable and expressive while still compiling down to regular JavaScript.
Each JSX expression is transformed into a call like React.createElement(). React then uses these elements to build and update the UI efficiently inside a target DOM node (usually a <div id="root">).
<br /> and <img />.className and htmlFor.{ } for dynamic values.if or for directly.When you write JSX, you are writing a more readable layer on top of React's element creation API. The JSX compiler checks the structure of your tags and transforms them into nested JavaScript objects that describe your UI.
<div> or <>...</> (React Fragment).onclick and tabindex become onClick and tabIndex in JSX.{ } to insert variables, function results, and simple calculations directly into your markup.if / for) must live in JavaScript, not directly in JSX markup.This example creates a simple React element and renders it into a DOM container using ReactDOM.
// Render a simple heading inside the root div
const element = <h1>Welcome to React!</h1>;
const container = ReactDOM.createRoot(document.getElementById("root"));
container.render(element);
React creates an element that represents <h1>Welcome to React!</h1> and mounts it into the DOM node with ID root. On the page, you will see a big heading saying "Welcome to React!".
JSX becomes powerful when you inject JavaScript values into your UI using curly braces. The values are evaluated at render time.
// Inject dynamic values into JSX using curly braces
const user = "Aarav";
const time = new Date().toLocaleTimeString();
const element = (
<div>
<h2>Hello, {user}!</h2>
<p>Current time: {time}</p>
</div>
);
When rendered, this JSX produces a <div> containing a personalized greeting and the current time, for example:
Hello, Aarav!
Current time: 4:15:32 PM
Any time the component re-renders, the expressions inside { } are re-evaluated so the UI always reflects the latest values.
JSX lets you apply classes and styles similar to HTML, but in a JavaScript-friendly way. CSS classes use className and inline styles are JavaScript objects.
// Apply a style object and className in JSX
const style = { color: "blue", fontSize: "20px" };
const element = <h1 style={style} className="heading">
Styled JSX Example
</h1>;
This code creates an <h1> element with blue text and a font size of 20px. The className="heading" can be targeted in your CSS file, while the style object gives you quick inline customization using JavaScript.
class instead of className for CSS classes.<img /> or <br />.if, for) directly inside JSX markup.<>...</> to group elements without adding extra nodes to the DOM.React.createElement() calls — the final result must always be valid JavaScript.ProfileCard that displays your name, age, and city using JSX.{ } to insert a JavaScript variable like currentYear into your JSX.Goal: Practice writing valid JSX, embedding JavaScript expressions, and applying attributes and styles while avoiding common JSX pitfalls.