React JSX

Write HTML-like markup in JavaScript.

✍️ 3 exercises + code challenge in this lesson

JSX lets you write HTML-like code in JavaScript. It is compiled into React.createElement() calls.

JSX rules

  • Return a single root element (wrap siblings in <>...</>, a Fragment).
  • Close every tag, including <img /> and <br />.
  • Use className instead of class, and htmlFor instead of for.
  • Put JavaScript expressions in curly braces: {2 + 2}, {user.name}.
  • Inline styles are objects: style={{ color: "red" }}.

Key Takeaways: React JSX

Mastering React JSX helps you write clean and reliable React code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

React JSX Code Example

React
<div id="root"></div>
<script src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.25.6/babel.min.js"></script>

<script type="text/babel">
const { useState, useEffect, useRef, useMemo, createContext, useContext } = React;
function App() {
  const name = "Sara";
  const lessons = 12;
  const avatar = "https://i.pravatar.cc/80?img=5";

  return (
    <>
      <img src={avatar} alt={name} style={{ borderRadius: "50%" }} />
      <h2 className="title">Welcome back, {name}!</h2>
      <p>You have finished {lessons} lessons ({Math.round(lessons / 20 * 100)}%).</p>
    </>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
</script>
Free sign-in required · Google login available

React JSX Practice Exercises

Check what you learned in “React JSX”.

/ 3 solved
Fill in the blank

In JSX, the class attribute is written as…

<div ="card">Hi</div>
✓ Correct! Not quite. Try again.
Multiple choice

How do you insert a JavaScript expression in JSX?

✓ Correct! Not quite. Try again.
Code challenge

Render "Hello, Sara" using the name variable inside curly braces in an <h2>.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services