🎓 A1Learner
🔍
0 of 50 lessons completed

JSX Expressions

A JSX expression is JavaScript code inside curly braces in JSX. The braces tell React to run the code and show the result.

A variable in JSX

The braces show the value of name.

const name = 'Ada'
return <h1>Hello, {name}!</h1>

A number in JSX

The braces show the result of 2 plus 3.

return <h1>{2 + 3}</h1>

Example

export default function App() {
  const name = 'Ada'
  return <h1>Hello, {name}!</h1>
}

Key points

  • An expression is code that works out to one value.
  • Curly braces mark the spot where the value goes.
  • A variable from the function can go inside the braces.
  • A number or a string can also go inside the braces.

In this example, App sets a variable named name. The variable holds the string Ada. The JSX puts the variable inside curly braces. React shows Hello, Ada! on the page.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

How does JSX show the value of a variable?