JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsreactJSX
PrevNext
react
beginner
8 min read

JSX

babel
conditional-rendering
fundamentals
jsx
syntax
xss

JSX is a syntax extension that lets you write HTML-like markup in JavaScript — it compiles to function calls (React.createElement or the automatic jsx runtime) and provides XSS protection by escaping values by default.

Key Points

1Compiled Syntax

JSX compiles to React.createElement() calls (or jsx() with React 17+ automatic runtime) — it's syntax sugar, not a template language.

2HTML Differences

className instead of class, htmlFor instead of for, camelCase events/attributes, required self-closing tags, and object-style inline styles.

3Expressions in Braces

Curly braces {} embed any JavaScript expression — variables, function calls, ternaries. Statements (if/else, for) cannot be used directly.

4Conditional Rendering

Ternary for if/else, logical AND (&&) for show/hide, early return for loading states, or null to render nothing.

5XSS Protection

JSX escapes all embedded values by default — user input is treated as text, not HTML. dangerouslySetInnerHTML is the explicit opt-out.

What You'll Learn

  • Explain what JSX is and how it compiles to JavaScript
  • Know the key differences between JSX and HTML syntax
  • Understand how JSX prevents XSS attacks by default

Deep Dive

JSX (JavaScript XML) is a syntax extension for JavaScript that lets you write HTML-like markup directly in your JavaScript code. It's not required to use React, but virtually all React code uses it because it makes component structure visual and intuitive.

How JSX Works

JSX is not valid JavaScript — it must be compiled. Before React 17, Babel transformed <div className="hello">Hi</div> into React.createElement('div', { className: 'hello' }, 'Hi'), which is why every file needed import React from 'react'. Since React 17, the automatic JSX transform compiles to _jsx('div', { className: 'hello', children: 'Hi' }) — no React import required. The compiled output creates JavaScript objects (React elements) that describe what should appear on screen.

Key Differences from HTML

  • className instead of class (because class is a reserved word in JavaScript)
  • htmlFor instead of for on labels (same reason)
  • Event handlers use camelCase: onClick, onChange, onSubmit (not onclick)
  • Self-closing tags are required: <img />, <input />, <br /> (HTML allows <img> but JSX doesn't)
  • style accepts an object with camelCase properties: style={{ backgroundColor: 'red', fontSize: '16px' }}
  • All attributes use camelCase: tabIndex, autoFocus, readOnly
  • JSX must return a single root element — use <>...</> (Fragment) to wrap multiple elements without adding a DOM node

Expressions in JSX

Curly braces {} embed any valid JavaScript expression: variables, function calls, ternary operators, array methods. Statements (if/else, for loops, switch) cannot be used directly inside JSX — use ternary expressions or extract logic before the return.

Conditional Rendering

Three common patterns:

  • Ternary: {isLoggedIn ? <Dashboard /> : <Login />}
  • Logical AND: {hasError && <ErrorMessage />} — renders nothing when hasError is falsy (but beware: {0 && <Component />} renders 0, not nothing — use {count > 0 && <Component />} instead)
  • Early return: if (!data) return <Loading />; before the main JSX
  • Return null to render nothing

XSS Protection

JSX escapes all embedded values by default — if you insert user input via {userInput}, it's treated as text, not HTML. This prevents cross-site scripting (XSS) attacks. To deliberately render HTML, you must use dangerouslySetInnerHTML={{ __html: sanitizedContent }} — the name is intentionally scary to discourage unsafe usage.

JSX is Just JavaScript

Since JSX compiles to function calls, you can assign JSX to variables, pass it as props, return it from functions, and put it in arrays. This is what makes React's composition model powerful — JSX elements are just values.

Key Interview Distinction

JSX is syntax sugar that compiles to React.createElement() (or jsx() with the automatic runtime). It is not a template language — it has the full power of JavaScript. The main differences from HTML are className, htmlFor, camelCase attributes, required self-closing tags, and object-style style props. JSX escapes values by default for XSS protection.

Fun Fact

When React was first introduced with JSX in 2013, many developers were horrified — mixing HTML-like syntax in JavaScript felt like a violation of separation of concerns. The backlash was so strong that the React team created a JSX-free API (React.createElement) as an alternative. Within a year, JSX became the standard and is now universally accepted.

Continue Learning

React Fundamentals

beginner

Components

beginner

Practice What You Learned

What is JSX and why does React use it?
junior
jsx
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.
What are React Fragments and when should you use them?
mid
fragments
React Fragments let you group multiple elements without adding an extra DOM node. Use the <Fragment> syntax when you need to pass a key prop, or the shorter <>...</> syntax when you don't.
Previous
React Internals
Next
UI Component Libraries
PrevNext