Lesson 12 of 60 – JSX Syntax
20%

JSX Syntax

JSX has a syntax that looks similar to HTML but follows JavaScript and React rules. Understanding JSX syntax is important for writing clean and error-free React components.

Note: JSX is HTML-like syntax used inside JavaScript. It must follow specific rules such as proper closing tags, one parent element, and JavaScript expressions inside curly braces.

1. Basic JSX Syntax

A simple JSX element can be written like an HTML element.

const element = <h1>Hello React</h1>;

Here, the h1 element is written inside JavaScript code.

2. JSX Must Be Properly Closed

Every normal JSX opening tag should have a corresponding closing tag.

<p>Welcome to React</p>

The opening tag is <p> and the closing tag is </p>.

3. Self-Closing JSX Tags

Elements that do not contain child content can use self-closing syntax.

<img src="logo.png" alt="Logo" />

<input type="text" />

<br />

The /> closes the JSX element.

4. JSX Needs One Parent Element

When a component returns multiple elements, they should be wrapped inside one parent element or a Fragment.

return (
    <div>
        <h1>Hello</h1>
        <p>Welcome</p>
    </div>
);

5. JSX Fragment Syntax

A Fragment can be used when you do not want to add an extra HTML element.

return (
    <>
        <h1>Hello</h1>
        <p>Welcome</p>
    </>
);

6. JSX Expressions

JavaScript expressions are written inside curly braces { }.

const name = "Amit";

return (
    <h1>Hello {name}</h1>
);

The value of name is displayed inside the heading.

7. Using Numbers in JSX

Numbers can be displayed inside JSX using curly braces.

const age = 25;

return (
    <p>Age: {age}</p>
);

8. JavaScript Calculations

You can perform calculations inside JSX expressions.

const price = 100;
const quantity = 3;

return (
    <p>Total: {price * quantity}</p>
);

The result displayed is 300.

9. Using Variables

Variables can be used inside JSX with curly braces.

const course = "React";

return (
    <h2>Learning {course}</h2>
);

10. JSX Attributes

JSX elements can have attributes.

<img
    src="photo.jpg"
    alt="Student"
 />

Attributes provide additional information to JSX elements.

11. className Attribute

In JSX, use className instead of the HTML class attribute.

<div className="container">
    <h1>React Tutorial</h1>
</div>

12. htmlFor Attribute

For a label element, JSX commonly uses htmlFor instead of HTML's for attribute.

<label htmlFor="email">
    Email
</label>

<input id="email" type="email" />

13. Boolean Attributes

Boolean attributes can be used in JSX to enable certain properties.

<input
    type="text"
    disabled
/>

The disabled property makes the input unavailable for user interaction.

14. Passing Boolean Values

A JavaScript boolean expression can also be passed using curly braces.

const isDisabled = true;

return (
    <button disabled={isDisabled}>
        Submit
    </button>
);

15. JSX Inline Styles

Inline styles in JSX use a JavaScript object.

const style = {
    color: "blue",
    fontSize: "24px"
};

return (
    <h1 style={style}>
        Hello React
    </h1>
);

16. Inline Style Object

The style object can also be written directly inside JSX.

<h1
    style={{
        color: "green",
        fontSize: "30px"
    }}
>
    React
</h1>

17. JSX Event Syntax

React events are written using camelCase names such as onClick.

<button onClick={handleClick}>
    Click Me
</button>

This differs from writing event attributes in lowercase HTML style.

18. JSX Comments

Comments inside JSX are written using JavaScript comment syntax inside curly braces.

return (
    <div>

        {/* This is a JSX comment */}

        <h1>Hello</h1>

    </div>
);

19. JSX String Values

String values can be written directly as attribute values.

<input
    type="text"
    placeholder="Enter your name"
/>

Curly braces are useful when the value comes from JavaScript.

20. JSX Attributes with Variables

JavaScript variables can be used as attribute values with curly braces.

const imageUrl = "photo.jpg";

return (
    <img
        src={imageUrl}
        alt="Profile"
    />
);

21. Nested JSX Elements

JSX elements can be nested inside other JSX elements.

<div>

    <h1>React Course</h1>

    <div>
        <p>Learn JSX</p>
    </div>

</div>

22. JSX Conditional Expression

The ternary operator can be used inside JSX for simple conditions.

const loggedIn = true;

return (
    <h2>
        {loggedIn ? "Welcome" : "Please Login"}
    </h2>
);

23. Logical AND in JSX

The logical AND operator can conditionally display JSX content.

const isAdmin = true;

return (
    <div>
        {isAdmin && <p>Admin Panel</p>}
    </div>
);

The paragraph is rendered when isAdmin is true.

24. JSX with Arrays

Arrays can be rendered using JavaScript expressions.

const names = ["Amit", "Ravi", "Neha"];

return (
    <div>
        {names.map(name => (
            <p key={name}>{name}</p>
        ))}
    </div>
);

25. JSX with Function Calls

JavaScript functions can be called inside JSX expressions.

function getName() {
    return "Rahul";
}

return (
    <h1>Hello {getName()}</h1>
);

26. JSX Parentheses

Parentheses are commonly used when JSX contains multiple lines.

return (
    <div>
        <h1>Welcome</h1>
        <p>Learn React</p>
    </div>
);

They make multiline JSX easier to read.

27. JSX Whitespace and Text

JSX handles text and whitespace according to its own rendering rules. For readable layouts, use CSS rather than relying on repeated spaces.

<p>
    Welcome to the React tutorial.
</p>

28. JSX Syntax Error Example

Forgetting to close a JSX element can cause a syntax error.

// Incorrect

return (
    <div>
        <h1>Hello
    </div>
);

The heading should be properly closed:

// Correct

return (
    <div>
        <h1>Hello</h1>
    </div>
);

29. JSX Syntax Best Practices

  • Close every JSX element properly.
  • Use self-closing syntax when appropriate.
  • Use one parent element or a Fragment.
  • Use className instead of class.
  • Use htmlFor for label associations.
  • Use camelCase for React event names such as onClick.
  • Use curly braces for JavaScript expressions.
  • Keep JSX properly formatted and readable.
  • Use meaningful variable and component names.

30. Complete JSX Syntax Example

function App() {

    const name = "Rahul";
    const course = "React";
    const loggedIn = true;

    function handleClick() {
        alert("Welcome " + name);
    }

    return (
        <div className="container">

            <h1>
                Hello {name}
            </h1>

            <p>
                You are learning {course}.
            </p>

            {loggedIn && (
                <p>You are logged in.</p>
            )}

            <button
                className="btn"
                onClick={handleClick}
            >
                Start Learning
            </button>

        </div>
    );

}

export default App;

This example combines variables, expressions, attributes, events, conditional rendering, and nested JSX elements.

📌 Key Points

  • JSX uses HTML-like syntax inside JavaScript.
  • Every normal JSX element should be properly closed.
  • Self-closing elements use />.
  • Multiple JSX elements normally need one parent element.
  • Fragments can group JSX without adding an extra HTML element.
  • JavaScript expressions are written inside { }.
  • Use className instead of class.
  • Use htmlFor instead of for for labels.
  • React event names such as onClick use camelCase.
  • JavaScript variables can be used in JSX attributes.
  • JSX can contain conditions, arrays, function calls, and calculations.
  • Proper formatting makes JSX easier to read and maintain.

🧠 Quick Quiz

Question: How do you write a JavaScript expression inside JSX?