React Tutorial menu

React JSX

JSX lets you write HTML-like code inside JavaScript, and it is how React components describe the screen.

What is JSX?

JSX stands for JavaScript XML. It looks like HTML, but it lives inside a JavaScript file.

Example

jsx
const heading = <h1>Welcome to Workownix</h1>;

The browser cannot read JSX directly. Vite turns it into normal JavaScript before the page loads. You do not need to do anything for this.

JavaScript Inside Curly Braces

Put any JavaScript expression inside { } and React shows its value.

Example

jsx
function Receipt() {
  const item = "School bag";
  const price = 1800;
  const quantity = 2;

  return (
    <div>
      <h2>{item}</h2>
      <p>Quantity: {quantity}</p>
      <p>Total: Rs {price * quantity}</p>
      <p>Date: {new Date(2026, 9, 15).toDateString()}</p>
    </div>
  );
}
Output
School bag
Quantity: 2
Total: Rs 3600
Date: Thu Oct 15 2026

An expression is anything that produces a value: a variable, a sum, a function call, a ternary a ? b : c. You cannot put statements like if or for directly inside { }.

Rule 1: Return One Parent Element

A component must return a single root element. This is wrong:

Example

jsx
// Error: two elements side by side
return (
  <h1>Title</h1>
  <p>Text</p>
);

Wrap them in a <div>, or in an empty fragment <> </> that adds nothing to the page:

Example

jsx
return (
  <>
    <h1>Title</h1>
    <p>Text</p>
  </>
);

Rule 2: Close Every Tag

In HTML some tags, like <img> and <br>, have no closing tag. In JSX every tag must be closed. Empty tags end with />.

Example

jsx
<img src="/logo.png" alt="Shop logo" />
<br />
<input type="text" />

Rule 3: Use camelCase Attributes

Some HTML attribute names are reserved words in JavaScript, so JSX uses different names.

HTMLJSX
classclassName
forhtmlFor
onclickonClick
tabindextabIndex
style="color: red"style={{ color: "red" }}

Inline Styles

In JSX the style attribute takes a JavaScript object. Property names use camelCase, so background-color becomes backgroundColor.

Example

jsx
function Banner() {
  const boxStyle = {
    backgroundColor: "#0b5ed7",
    color: "white",
    padding: "12px",
  };

  return <div style={boxStyle}>Admissions open for the new batch!</div>;
}

The outer braces mean "JavaScript here", and the inner braces are the object itself. That is why you see {{ }} when the object is written directly.

Attributes With Values From Variables

You can use curly braces for attribute values too:

Example

jsx
const photo = "/images/ayesha.jpg";
const name = "Ayesha";

const card = <img src={photo} alt={"Photo of " + name} width={120} />;

Do not put quotes around the braces. src="{photo}" would show the text {photo} instead of the value.

Comments in JSX

Inside JSX, write comments inside braces:

Example

jsx
return (
  <div>
    {/* This is a comment and is not shown */}
    <p>Visible text</p>
  </div>
);

If something looks wrong, check the browser console. React gives clear warnings, for example when you write class instead of className.

Exercise

Exercise

Which attribute do you use in JSX to give an element a CSS class?

Learn React with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp