Loading Inter font-family
Loading Josephine Sans font-family
Loading Hallywood font-family

The Myth You Should Know About && (Short Circuit Syntax) in JSX

Showrin Barua
Showrin Barua / April 12, 2021
4 min read β€’ ----

The Myth You Should Know About && (Short Circuit Syntax) in JSX by Showrin

While writing code in React, specifically in JSX, most of the junior developers (including me 😢), have a misconception regarding && syntax. Sometimes, this misconception leads to some unavoidable bugs.

The Misconception

Have a look at the following code block.

let printHello = true;

printHello && console.log('Hello');

printHello = false;

printHello && console.log('Hello');

Can you guess the output of this code block? Most of the beginners will say the output will be β€”

Hello

For line 3, it will print "Hello" and nothing for line 7. But that's not right. The output will be β€”

Hello
false

And that’s the misconception, most of the beginners have regarding && syntax.

Right Concept

What actually happened in the above code block is β€”

  1. Compiler executed the first statement printHello and checked whether the returned value of this statement is true or false

  2. When true, it executed the second statement console.log("Hello")

  3. When false, it didn’t log in to the console and returned the value of printHello i.e false

So && syntax will execute the 2nd argument if the first one returns true, otherwise, it will execute only the 1st one.

How JSX evaluate && syntax

JSX avoids the boolean values/undefined/null to be rendered inside a dom element. Source That means if you write β€”

<div>{false}</div>
<div>{undefined}</div>
<div>{null}</div>

It will render these divs with no value.

Let think about the following React code block.

import { StrictMode } from 'react';
import ReactDOM from 'react-dom';

const rootElement = document.getElementById('root');
const isTure = true;
const isFalse = false;
const isUndefined = undefined;
const isNull = null;

function App(props) {
  return <div className="App">{props.children}</div>;
}

ReactDOM.render(
  <StrictMode>
    <App>
      <h1>Hello, I am a child</h1>
      <h2>I am also a child</h2>
      {isTure && <div>I will be rendered when the first statement is true</div>}
      {isFalse && <h2>I will be rendered when the first statement is true</h2>}
      {isUndefined && <h2>I will be rendered when the first statement is true</h2>}
      {isNull && <h2>I will be rendered when the first statement is true</h2>}
    </App>
  </StrictMode>,
  rootElement
);

It will render only the first 3 elements. Because the last 3 have false, undefined, and null as the output of the first statement. If you check out the React Dev Tools (I attached the environment at the end of this article. You can test it there), you will see false, undefined, and null as the child of App Component.

rendering process of jsx

But JSX ignores these children (false, undefined, and null) while rendering.

Problem Happens Due to The Misconception

Due to the misconception regarding && syntax, most of the devs think that if we put a falsy value as the first statement, JSX will return nothing. This thinking is partially true. Because, if the falsy value is a number like 0 (while checking the length of an array is 0 or not), then JSX will avoid the second statement but not the 0. Then you will find the 0 in your browser like the following.

problems in jsx

I think this is a common problem every React Dev experiences.

So, always try to put boolean values as the first statement for && syntax in JSX. Because JSX will render number even it is falsy. You can simply do it β€”

Instead of arr.length && <div> not empty </div>, you have to write arr.length > 0 && <div> not empty </div>.

Here’s the environment of the above React code block β€”

Resources

Thank you for reading. If you find this article useful and like it, feel free to share it so more people can take benefit from it. You can connect with me and discuss this article on LinkedIn or my website.

Happy Coding πŸŽ‰πŸŽ‰πŸŽ‰

ReactJsxJavaScript

Stay Tuned To Get Latests