🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

JSX

AI & DATA SCIENCE // jsx

JSX is a syntax extension for JavaScript that lets you write HTML-like markup directly inside JavaScript code, which gets compiled into plain JavaScript function calls.

Syntax

const element = <h1 className="title">Hello!</h1>;

Deep Dive Course

JSX looks like HTML embedded in JavaScript, but it's actually syntactic sugar that a compiler, like Babel, transforms into plain JavaScript calls to React.createElement(), or an equivalent automatic runtime function in modern React, before the browser ever sees it — <h1>Hello!</h1> compiles down to something functionally equivalent to React.createElement('h1', null, 'Hello!'). Because it compiles to plain JavaScript, JSX lets you embed real JavaScript expressions directly inside curly braces, {expression}, mixing markup and logic together far more naturally than working with createElement() calls by hand.

1Understanding JSX

JSX looks like HTML embedded in JavaScript, but it's actually syntactic sugar that a compiler, like Babel, transforms into plain JavaScript calls to React.createElement(), or an equivalent automatic runtime function in modern React, before the browser ever sees it — <h1>Hello!</h1> compiles down to something functionally equivalent to React.createElement('h1', null, 'Hello!'). Because it compiles to plain JavaScript, JSX lets you embed real JavaScript expressions directly inside curly braces, {expression}, mixing markup and logic together far more naturally than working with createElement() calls by hand.

💡

Since JSX ultimately compiles down to plain JavaScript function calls, anything you can do in JSX you could technically also write by hand with React.createElement() directly — JSX just makes it dramatically more readable.

editor.html
const name = 'Ana';
const element = <h1>Hello, {name}!</h1>;
console.log(element.type, element.props.children);
localhost:3000

2Practical Example

Here is a real-world application of JSX showing how it is used in production React code.

editor.html
function List() {
  const items = ['Apples', 'Bananas', 'Cherries'];
  return (
    <ul>
      {items.map(item => <li key={item}>{item}</li>)}
    </ul>
  );
}
localhost:3000

3Best Practices

Follow these guidelines when working with JSX:

1. Wrap JavaScript expressions you want to embed in JSX inside curly braces, like {user.name}, remembering that statements like if or for cannot go directly inside JSX this way

2. Use className instead of class, and camelCase for other attributes like onClick, since JSX attributes map to JavaScript/DOM property names, not raw HTML attribute names

3. Always return a single root element, or a Fragment, from a component, since JSX cannot return multiple sibling elements directly without one

⚠️

Tip: Since JSX ultimately compiles down to plain JavaScript function calls, anything you can do in JSX you could technically also write by hand with React.createElement() directly — JSX just makes it dramatically more readable.

editor.html
const name = 'Ana';
const element = <h1>Hello, {name}!</h1>;
console.log(element.type, element.props.children);
localhost:3000

4Attributes: className and style Objects

JSX attributes map to JavaScript/DOM property names, not raw HTML attribute strings. class becomes className, for becomes htmlFor, onclick becomes onClick, and style takes a JavaScript object of camelCased CSS properties instead of a semicolon-separated string.

editor.html
<div
  className="card"
  style={{ backgroundColor: 'white', fontSize: 14 }}
  onClick={handleClick}
>
localhost:3000

5Automatic Escaping Prevents Injection

Any value rendered through JSX's curly braces is escaped before being inserted into the DOM. A user-supplied string like <script>alert(1)</script> rendered as {comment.text} shows up as literal, harmless text on the page rather than being parsed as an actual tag — this is what makes JSX safe against XSS by default.

editor.html
const comment = { text: '<script>alert(1)</script>' };
return <p>{comment.text}</p>;
localhost:3000

Examples

Example 01Basic Usage
const name = 'Ana';
const element = <h1>Hello, {name}!</h1>;
console.log(element.type, element.props.children);
Example 02Advanced Example
function List() {
  const items = ['Apples', 'Bananas', 'Cherries'];
  return (
    <ul>
      {items.map(item => <li key={item}>{item}</li>)}
    </ul>
  );
}

Best Practices

  • Wrap JavaScript expressions you want to embed in JSX inside curly braces, like {user.name}, remembering that statements like if or for cannot go directly inside JSX this way
  • Use className instead of class, and camelCase for other attributes like onClick, since JSX attributes map to JavaScript/DOM property names, not raw HTML attribute names
  • Always return a single root element, or a Fragment, from a component, since JSX cannot return multiple sibling elements directly without one
  • Trust JSX's automatic escaping for rendered text instead of writing manual sanitization — reserve dangerouslySetInnerHTML for the rare case of genuinely trusted, pre-sanitized HTML

Interview Question

Why can you write {condition ? <A /> : <B />} directly inside JSX, but not an if/else statement in the same spot?

Hint: Think about what curly braces in JSX are actually allowed to contain — expressions, or statements.

Curly braces in JSX can only contain a JavaScript expression, something that evaluates to a single value, because JSX compiles each embedded {...} directly into an argument or child value passed into the underlying React.createElement()-style function call, and a function argument position can only ever hold a value, not a statement. A ternary expression, condition ? <A /> : <B />, is a single expression that evaluates to one of two values, making it perfectly valid in that position. An if/else statement, by contrast, is a control-flow statement that doesn't itself evaluate to any value at all, it just directs which block of code runs, so it has nothing meaningful to substitute into that argument slot — this is exactly why conditional rendering in JSX relies on ternaries, logical &&, or moving the if/else logic outside the JSX and into a plain variable beforehand, rather than writing if/else directly inside the braces.

Exercises

MediumPractice using JSX in a real scenario.
View Solution
const name = 'Ana';
const element = <h1>Hello, {name}!</h1>;
console.log(element.type, element.props.children);

Frequently Asked Questions

Why can you write {condition ? <A /> : <B />} directly inside JSX, but not an if/else statement in the same spot?
Does rendering user-supplied text in JSX require manually escaping it to prevent XSS?