#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JSX

Introduction to JSX

JSX is a syntax extension for JavaScript that lets you write markup that looks like HTML directly inside your React code. It describes what the user interface should look like, and a build tool such as Babel or esbuild turns it into ordinary JavaScript function calls before the browser runs it. Almost every React component you write will return JSX.

What JSX compiles into

JSX is not a string and it is not HTML. Each tag becomes a function call that creates a plain JavaScript object describing an element. With the modern JSX transform, <h1 className="title">Hello</h1> becomes roughly jsx('h1', { className: 'title', children: 'Hello' }). React reads those objects and updates the real DOM to match.

Because the result is just a value, you can store JSX in variables, return it from functions, put it in arrays, and pass it as a prop.

A first component in JSX

function WelcomeCard({ name, unread }) {
  const greeting = `Welcome back, ${name}!`;

  return (
    <div className="card">
      <h2>{greeting}</h2>
      <p>You have {unread} new {unread === 1 ? 'message' : 'messages'}.</p>
      <label htmlFor="search">Search inbox</label>
      <input id="search" type="text" />
    </div>
  );
}

The curly braces are the bridge back into JavaScript. Inside them you can place any expression, something that produces a value: a variable, a function call, arithmetic, or a ternary like the one above.

How JSX differs from HTML

  • className, not class. class is a reserved word in JavaScript, so React uses className. Similarly, for on labels becomes htmlFor.
  • camelCase attributes. Event handlers and most attributes are camelCase: onClick, tabIndex, maxLength.
  • Every tag must close. Empty elements need a slash: <input />, <img />, <br />.
  • Inline styles are objects. Write style={{ fontSize: 18 }} with camelCase keys, not a CSS string.
  • Comments go inside braces: {/* note */}.

One parent per return

A component can return only one top-level element, because a function returns a single value. If you need two siblings without an extra wrapper div, use a Fragment, written as empty tags <>...</>. Fragments group elements without adding anything to the DOM.

The parentheses around a multi-line return are there for a practical reason. Without them, if the JSX starts on the line after return, JavaScript's automatic semicolon insertion ends the statement early and the function returns undefined.

A classic mistake: statements inside braces

Curly braces accept expressions only. Writing {if (loggedIn) { ... }} or a for loop inside JSX is a syntax error. Use a ternary ({loggedIn ? <Logout /> : <Login />}), the && operator, or array.map() instead, or compute the value in a variable above the return.

Also note that JSX escapes values placed in braces. If a user's name contains <script>, React shows it as text rather than running it, which protects against a whole class of injection attacks.

Component names must be capitalised

React treats lowercase tags such as <div> as built-in HTML elements and capitalised tags such as <WelcomeCard /> as your components. Naming a component welcomeCard makes React look for an HTML tag that does not exist.

To remember: JSX is JavaScript that looks like HTML; braces hold expressions; attributes are camelCase; and every component returns a single root.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course