Course topics

By WebNest Studio Engineering Team

React Tutorial

JSX Syntax in React

JSX (JavaScript XML) is a syntax extension that lets you write HTML-like markup directly inside JavaScript. It is not a separate template language and it is not required to use React, but it is by far the most common way to describe UI in React applications because it reads almost exactly like the HTML it produces.

Under the hood, JSX is not understood by browsers at all. A build tool such as Babel transforms every JSX element into a plain JavaScript function call — historically `React.createElement(type, props, ...children)`, and in modern tooling the newer automatic JSX runtime (`_jsx`/`jsx` from `react/jsx-runtime`). Either way, JSX is purely syntax sugar: `<h1>Hello</h1>` compiles down to a function call that returns a plain JavaScript object describing that element, which React then uses to build and update the actual DOM.

Why it matters

JSX is the syntax in which every React component is written. It looks like HTML, and the places where it differs are exactly where beginners lose time: className instead of class, braces for JavaScript, and the rule that a component returns one root element.

JSX Compiles to Function Calls

Every piece of JSX you write is converted at build time into a call that creates a React element object. Understanding this equivalence explains many JSX rules that otherwise look arbitrary.

  • <h1>Hello</h1> compiles roughly to React.createElement("h1", null, "Hello").
  • A React element is a plain JavaScript object with a type, props, and children — it is not a DOM node.
  • Because JSX is just function calls, you can freely mix it with normal JavaScript: variables, conditionals, and loops all work around it.

Embedding Expressions with Curly Braces

Anything inside curly braces {} in JSX is evaluated as a JavaScript expression, and the result is inserted into the output. This works for variables, function calls, arithmetic, ternaries, and object property access — but not for statements like if or for, since those are not expressions. Attribute values also accept {} for dynamic values, for example <img src={imageUrl} />.

The Single Root Element Rule

A JSX expression must resolve to exactly one root element, because it ultimately becomes a single function call returning a single element object. Returning two sibling elements without a wrapper is a compile error. React provides a lightweight fragment, written as <>...</> or explicitly <React.Fragment>...</React.Fragment>, to group multiple elements without adding an extra DOM node like a <div>.

JSX Attribute Differences from HTML

JSX attributes use camelCase for most DOM properties (onClick, tabIndex, className instead of class) because they map to JavaScript object properties, not HTML attribute strings. Self-closing tags must include the trailing slash, for example <br /> and <img />, even for elements that are optional in raw HTML.

Example

JSX expressions, attributes, and a single wrapping fragment

JavaScript
function Greeting({ name, isLoggedIn }) {
  const currentHour = new Date().getHours();
  const greetingWord = currentHour < 12 ? 'Good morning' : 'Good evening';

  return (
    <>
      <h1 className="greeting-title">
        {greetingWord}, {name}!
      </h1>
      {isLoggedIn && <p>You are logged in.</p>}
    </>
  );
}

export default function App() {
  return <Greeting name="Asha" isLoggedIn={true} />;
}
Output
Renders an <h1> reading "Good morning, Asha!" (or "Good evening, Asha!" depending on the time), followed by a paragraph "You are logged in." because isLoggedIn is true — with no extra wrapping DOM element thanks to the fragment.

Common Mistakes

  • Returning two sibling elements from a component without wrapping them in a single parent or fragment, causing a "JSX expressions must have one parent element" error.
  • Using the HTML attribute "class" instead of the JSX prop "className" — React will silently ignore "class" on DOM elements.
  • Trying to put a JavaScript statement like an if block directly inside curly braces — curly braces only accept expressions, so conditionals must be rewritten as ternaries or moved outside the JSX.
  • Forgetting that JSX compiles to function calls, then being confused about why a component must return a single value.

Exercise

Do this in src/App.jsx of a Vite + React project. The component below has four JSX mistakes: it returns two sibling elements, uses class, puts a JavaScript value in quotation marks, and leaves an img tag unclosed. Fix them.

Expected result: the page shows "Hello, Asha", a paragraph with the class intro, and an image, with no errors in the console.

JavaScript · starter code
export default function App() {
  const name = "Asha";

  return (
    <h1>Hello, "name"</h1>
    <p class="intro">Welcome back.</p>
    <img src="/logo.png" alt="Logo">
  );
}
Show hints
  • Wrap sibling elements in a fragment, <>...</>, and write a JavaScript value inside braces: {name}.
  • In JSX the attribute is className, and every tag must be closed: <img ... />.
Show solution
JavaScript · solution
export default function App() {
  const name = "Asha";

  return (
    <>
      <h1>Hello, {name}</h1>
      <p className="intro">Welcome back.</p>
      <img src="/logo.png" alt="Logo" />
    </>
  );
}

Quick quiz

1. How is a JavaScript expression embedded in JSX?

  • A. Inside double quotation marks
  • B. Inside parentheses
  • C. Inside curly braces
  • D. Inside angle brackets
Show answer

C. For example, <h1>{user.name}</h1>.

2. Which attribute sets a CSS class in JSX?

  • A. className
  • B. class
  • C. cssClass
  • D. styleName
Show answer

A. class is a reserved word in JavaScript, so JSX uses the DOM property name.

3. What must a component return?

  • A. Exactly two elements
  • B. A string only
  • C. An array of strings
  • D. A single root element, which can be a fragment
Show answer

D. A fragment groups elements without adding a node to the DOM.

Interview questions

What is JSX?

Show answer

JSX is a syntax extension that lets markup be written inside JavaScript. It is not HTML and browsers cannot run it; a compiler turns each element into a function call that creates a plain JavaScript object describing the element. Because it is JavaScript, any expression can be embedded in braces, and the result can be stored in variables, passed to functions and returned from them.

How does JSX differ from HTML?

Show answer

Attributes use camelCase and DOM property names: className, htmlFor, onClick, tabIndex. Every tag must be closed, including img and input. The style attribute takes an object, not a string. JavaScript expressions go in braces. A component must return a single root element. Values rendered through braces are escaped, which protects against injected markup.

Key Points to Remember

  • JSX is syntax sugar that compiles to React.createElement (or the automatic JSX runtime) calls, producing plain JavaScript objects.
  • Curly braces {} embed any valid JavaScript expression into JSX markup or attributes.
  • A component must return one root element; use fragments (<>...</>) to group siblings without adding extra DOM nodes.
  • JSX attributes use camelCase and map to DOM properties, not raw HTML attribute strings.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of plain HTML, CSS and JavaScript. JSX needs a React project on your computer. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering