codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

3 / 61 topics
1Introduction to React.js2Setting Up Your Development Environment3JSX: Basics and Syntax4Components Introduction5Functional Components6Class Components7Props: Introduction and Usage8State: Introduction and Usage9Event Handling in React
Tutorials/React.js/JSX: Basics and Syntax
⚛️React.js

JSX: Basics and Syntax

Updated 2026-04-20
2 min read

Introduction

JSX (JavaScript XML) is a syntax extension for JavaScript that looks similar to HTML. It's used with React to describe what the UI should look like. JSX makes it easier to write templates and improves code readability by allowing you to write HTML-like code within your JavaScript files.

In this tutorial, we'll cover the basics of JSX, its syntax, and how it integrates with React components. We'll also discuss best practices for using JSX in your projects.

What is JSX?

JSX allows you to write HTML-like structures directly in your JavaScript code. It's not a string or template language; instead, it's transformed into React.createElement() calls during the build process. This transformation is handled by tools like Babel.

Here’s a simple example of JSX:

const element = <h1>Hello, world!</h1>;

This JSX code is compiled to:

const element = React.createElement('h1', null, 'Hello, world!');

Basic Syntax

Embedding Expressions

You can embed JavaScript expressions inside JSX by wrapping them in curly braces {}. This allows you to dynamically insert values into your UI.

function formatName(user) {
  return user.firstName + ' ' + user.lastName;
}

const user = { firstName: 'John', lastName: 'Doe' };

const element = <h1>Hello, {formatName(user)}!</h1>;

Attributes

JSX elements can have attributes similar to HTML. You can use camelCase for attribute names that are not reserved words.

const element = (
  <img src="avatar.png" alt="Profile Picture" />
);

For attributes that are reserved words in JavaScript (like class), you should use the camelCase equivalent (className).

const element = (
  <div className="container">
    <h1>Welcome to React</h1>
  </div>
);

Children

You can nest JSX elements within each other to create a hierarchy of components.

const element = (
  <div>
    <h1>Title</h1>
    <p>This is a paragraph.</p>
  </div>
);

Best Practices

Use Parentheses for Multiline JSX

When writing multiline JSX, wrap it in parentheses to avoid automatic semicolon insertion issues.

const element = (
  <div>
    <h1>Title</h1>
    <p>This is a paragraph.</p>
  </div>
);

Keep JSX Simple

Avoid putting too much logic inside your JSX. Instead, use JavaScript expressions and functions to keep your JSX clean and readable.

function getGreeting(user) {
  if (user) {
    return <h1>Welcome back, {formatName(user)}!</h1>;
  }
  return <h1>Please sign up.</h1>;
}

Use Fragments for Multiple Elements

If you need to return multiple elements from a component without wrapping them in an extra DOM element, use React Fragments.

function App() {
  return (
    <>
      <h1>Title</h1>
      <p>This is a paragraph.</p>
    </>
  );
}

Avoid Inline Styles

While you can use inline styles in JSX, it's generally better to keep your styles separate using CSS or styled-components.

const element = <div style={{ color: 'red', fontSize: '20px' }}>Styled Text</div>;

Advanced JSX Features

Conditional Rendering

You can conditionally render elements using ternary operators or logical && expressions.

function Greeting(props) {
  const isLoggedIn = props.isLoggedIn;
  return (
    <div>
      {isLoggedIn ? (
        <h1>Welcome back!</h1>
      ) : (
        <h1>Please sign up.</h1>
      )}
    </div>
  );
}

Lists and Keys

When rendering lists of items, use the map() function to create elements from an array. Each element should have a unique key prop.

const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
  <li key={number.toString()}>
    {number}
  </li>
);

return (
  <ul>{listItems}</ul>
);

Conclusion

JSX is a powerful feature of React that makes it easier to write and understand UI code. By following the best practices outlined in this tutorial, you can create clean, maintainable, and efficient React components.

Remember that JSX is just syntactic sugar for React.createElement() calls, so understanding how these work under the hood will help you write more effective React applications.


PreviousSetting Up Your Development EnvironmentNext Components Introduction

Recommended Gear

Setting Up Your Development EnvironmentComponents Introduction