JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsreact
Next

Learn the concept

JSX

react
junior
jsx

What is JSX and why does React use it?

jsx
syntax
fundamentals
jsx-transform
Quick Answer

JSX is a syntax extension that allows writing HTML-like code in JavaScript. React uses it because it provides a familiar, readable way to describe UI structure while leveraging JavaScript's full power for logic and expressions.

Detailed Explanation

What is JSX:

  • JavaScript XML - a syntax extension for JavaScript
  • Looks like HTML but gets compiled to JavaScript function calls
  • Not required for React but recommended for readability

How JSX Works:

  • JSX gets compiled to function calls that create React elements
  • Modern transform (React 17+): Compiles to jsx() / jsxs() from react/jsx-runtime — no need to import React in files that only use JSX
  • Legacy transform: Compiled to React.createElement() calls (required import React in every file)
  • Each JSX element becomes a plain JavaScript object describing the UI — this is called a 'React element'

Key Differences from HTML:

  • Use className instead of class
  • Use htmlFor instead of for
  • Style accepts objects, not strings
  • All tags must be closed (including self-closing)
  • camelCase for event handlers (onClick, onChange)

Benefits:

  • Visual representation of UI structure
  • Compile-time error checking
  • Full JavaScript expressions within {}

Code Examples

JSX basics and transformationJSX
// JSX syntax
const element = (
  <div className="greeting">
    <h1>Hello, World!</h1>
    <p>Welcome to React</p>
  </div>
);

// Modern transform (React 17+) — compiles to:
import { jsx as _jsx, jsxs as _jsxs } from 'react/jsx-runtime';
const element = _jsxs('div', {
  className: 'greeting',
  children: [
    _jsx('h1', { children: 'Hello, World!' }),
    _jsx('p', { children: 'Welcome to React' })
  ]
});
// No need to import React! The transform is automatic.

// Legacy transform (pre-React 17) compiled to:
// React.createElement('div', { className: 'greeting' }, ...)

Real-World Applications

Use Cases

Component Library Development

Using JSX to build reusable UI component libraries with consistent APIs

Server-Side Rendering

JSX compiles to function calls that work identically on server and client for SSR/SSG

Design System Implementation

Translating Figma designs to JSX component trees with design tokens

Mini Projects

JSX Expression Playground

beginner

Build a component that demonstrates all JSX expression types (conditionals, loops, embedding expressions)

Custom JSX Pragma

intermediate

Configure a custom JSX runtime to understand how JSX compilation works under the hood

Industry Examples

Airbnb

Developed their own ESLint JSX accessibility rules (eslint-plugin-jsx-a11y)

Vercel

Uses JSX in Next.js for both server and client component rendering

Resources

React Docs - Writing Markup with JSX

docs

React Docs - JavaScript in JSX with Curly Braces

docs

Related Questions

What is the difference between functional and class components in React?

junior
components
Next
What is the difference between functional and class components in React?
Next