Learnitweb

How to Print false Values in React?

In React, falsy values (false, null, undefined, 0, NaN, "") are not rendered when used inside JSX. If you directly try to display a false value inside JSX, React skips it instead of rendering anything.

Example: Falsy values don’t render

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Example = () => {
return <div>{false}</div>; // Nothing will be displayed
};
const Example = () => { return <div>{false}</div>; // Nothing will be displayed };
const Example = () => {
  return <div>{false}</div>; // Nothing will be displayed
};

1. Using String Conversion (String() or .toString())

To explicitly print false, convert it to a string.

Example: Using String()

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Example = () => {
return <div>{String(false)}</div>; // Prints: false
};
const Example = () => { return <div>{String(false)}</div>; // Prints: false };
const Example = () => {
  return <div>{String(false)}</div>; // Prints: false
};

Example: Using .toString()

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Example = () => {
return <div>{false.toString()}</div>; // Prints: false
};
const Example = () => { return <div>{false.toString()}</div>; // Prints: false };
const Example = () => {
  return <div>{false.toString()}</div>; // Prints: false
};

Best for explicitly showing true or false.

2. Using Conditional Rendering (JSON.stringify())

JSON.stringify() converts false, null, and undefined into printable strings.

Example: Using JSON.stringify()

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Example = () => {
return <div>{JSON.stringify(false)}</div>; // Prints: false
};
const Example = () => { return <div>{JSON.stringify(false)}</div>; // Prints: false };
const Example = () => {
  return <div>{JSON.stringify(false)}</div>; // Prints: false
};

Best when dealing with complex data types (e.g., objects, arrays).

3. Using a Ternary Operator

You can use a ternary operator to explicitly render "false" as a string.

Example: Rendering false as text

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Example = () => {
return <div>{false ? "true" : "false"}</div>; // Prints: false
};
const Example = () => { return <div>{false ? "true" : "false"}</div>; // Prints: false };
const Example = () => {
  return <div>{false ? "true" : "false"}</div>; // Prints: false
};

Best when rendering conditions dynamically.

4. Using Template Literals

Wrap the false value inside a template literal (backticks “ `).

Example: Using a Template Literal

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Example = () => {
return <div>{`${false}`}</div>; // Prints: false
};
const Example = () => { return <div>{`${false}`}</div>; // Prints: false };
const Example = () => {
  return <div>{`${false}`}</div>; // Prints: false
};

Simple and effective solution.

5. Using a Wrapper Function

If you frequently need to display boolean values, create a helper function.

Example: Boolean Rendering Helper

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const renderBoolean = (value) => (value === false ? "false" : value);
const Example = () => {
return <div>{renderBoolean(false)}</div>; // Prints: false
};
const renderBoolean = (value) => (value === false ? "false" : value); const Example = () => { return <div>{renderBoolean(false)}</div>; // Prints: false };
const renderBoolean = (value) => (value === false ? "false" : value);

const Example = () => {
  return <div>{renderBoolean(false)}</div>; // Prints: false
};

Best for reusable logic.

Conclusion

MethodCode ExampleOutput
String(false){String(false)}false
false.toString(){false.toString()}false
JSON.stringify(false){JSON.stringify(false)}false
Ternary Operator{false ? "true" : "false"}false
Template Literals{${false}}false
Wrapper Function{renderBoolean(false)}false