Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære JSX Embedding Expressions | Getting Started: JSX
Introduction to React

bookJSX Embedding Expressions

One of the main advantages of JSX is its capability to incorporate expressions directly into the code, enabling the inclusion of dynamic values within components or elements.

To include an expression in JSX, we enclose it within curly braces {}. This notation indicates to JSX that the contents should be treated as JavaScript.

const name = "Josh Perez"; 
const heading = <h1>Hello, {name}!</h1>;

The displayed value for the variable heading will be: Hello, Josh Perez! Similarly, you can do something more complicated and display the evaluated sum of two variables:

const x = 7;
const y = 10;
const sentence = <p>The sum of {x} and {y} is {x + y}</p>

The output for the above expression will be: The sum of 7 and 10 is 17 It is also possible to embed ternary operators which can show conditional output:

const score = 90;
const remarks = <p>You have {score > 50 ? "passed" : "failed"} the test.</p>

The output for the above expression will be: You have passed the test.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 2.7

bookJSX Embedding Expressions

Sveip for å vise menyen

One of the main advantages of JSX is its capability to incorporate expressions directly into the code, enabling the inclusion of dynamic values within components or elements.

To include an expression in JSX, we enclose it within curly braces {}. This notation indicates to JSX that the contents should be treated as JavaScript.

const name = "Josh Perez"; 
const heading = <h1>Hello, {name}!</h1>;

The displayed value for the variable heading will be: Hello, Josh Perez! Similarly, you can do something more complicated and display the evaluated sum of two variables:

const x = 7;
const y = 10;
const sentence = <p>The sum of {x} and {y} is {x + y}</p>

The output for the above expression will be: The sum of 7 and 10 is 17 It is also possible to embed ternary operators which can show conditional output:

const score = 90;
const remarks = <p>You have {score > 50 ? "passed" : "failed"} the test.</p>

The output for the above expression will be: You have passed the test.

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt