#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

Conditional Constructs

Conditional rendering in JSX means showing different elements depending on some value, such as whether a user is logged in, whether data has loaded or whether a list is empty. JSX itself has no if statement, because everything inside curly braces must be an expression. Instead you use plain JavaScript: if statements before the return, the ternary operator, the logical && operator, and early returns.

Using if before the return

When the choice affects large parts of the output, an ordinary if outside the JSX is often the clearest option. You can return early, or store elements in a variable and use it later.

function OrderStatus({ order }) {
  if (!order) {
    return <p>Loading your order...</p>;
  }

  let badge;
  if (order.status === 'shipped') {
    badge = <span className="badge ok">Shipped</span>;
  } else if (order.status === 'cancelled') {
    badge = <span className="badge error">Cancelled</span>;
  } else {
    badge = <span className="badge">Processing</span>;
  }

  return (
    <div>
      <h2>Order #{order.id}</h2>
      {badge}
    </div>
  );
}

Returning null from a component renders nothing, which is a clean way to hide a component entirely.

Ternary for either-or inside JSX

The ternary operator condition ? a : b is an expression, so it fits inside curly braces. Use it when you must show one thing or another.

function LoginButton({ isLoggedIn, onLogin, onLogout }) {
  return (
    <header>
      {isLoggedIn ? (
        <button onClick={onLogout}>Log out</button>
      ) : (
        <button onClick={onLogin}>Log in</button>
      )}
      <p className={isLoggedIn ? 'status online' : 'status'}>
        {isLoggedIn ? 'Welcome back' : 'Please sign in'}
      </p>
    </header>
  );
}

Ternaries also work inside attributes, as the className above shows.

Logical and for show-or-hide

When there is nothing to show in the "false" case, condition && <Element /> is shorter. If the condition is truthy, React renders the element; if it is false, null or undefined, it renders nothing.

The zero that appears on screen

This is the classic bug with &&. Writing {items.length && <List items={items} />} shows a lone 0 on the page when the list is empty. The && operator returns the left value when it is falsy, and React renders numbers, including zero. Booleans are not rendered, so make the left side a real boolean: {items.length > 0 && ...} or {!!items.length && ...}. The same issue happens with NaN.

Another readability trap is nesting ternaries several levels deep. It works, but it is hard to follow. Move the logic to an if chain before the return, or into a small helper component.

Picking an approach

SituationGood choice
Whole component depends on a state like loadingEarly return
Show A or BTernary
Show A or nothing&& with a boolean on the left
Three or more casesif...else chain, switch or an object lookup before the return

You can also swap whole components with an object lookup instead of a long chain. For example, a views object that maps 'list', 'grid' and 'map' to components lets you write const View = views[mode] and then render <View />. Give it a fallback, such as views[mode] ?? ListView, so an unexpected value does not crash the render. The variable name must start with a capital letter for JSX to treat it as a component.

Main points

JSX accepts expressions, so conditions are written with JavaScript you already know. Use early returns for big branches, ternaries for either-or choices and && for optional pieces, and always keep the left side of && a boolean so a stray zero never shows up.

Ready to master real-world web development?

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

Explore Course