Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Conditional line break

I only want to line break if variable var is not a null value, how would I go about this?

<div>
{var && `${var}`} <br />
{var2 && `${var2}`}
</div>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Like any other conditional element

<div>
  {var && `${var}`}
  {var && <br />}
  {var2 && `${var2}`}
</div>

Or shorter,

<div>
  {var && <>{`${var}`}<br /></>}
  {var2 && `${var2}`}
</div>
about 4 years ago · Juan Pablo Isaza Report

0

If you are only checking for null

{var !== null ? '<br />' : ''}

If any falsy statement is the condition you want to check (undefined , null , NaN , 0 , "" , and false)

{var ? '<br />' : ''}
about 4 years ago · Juan Pablo Isaza Report

0

You are creating HTML in JavaScript, so I assume you have used back-ticks around opening and closing div first. Then based on the value of var, You want to set the break point.

First thing is var is a keyword in Javascript and should not be used as the variable name.

Second is we can do something like this using ES6 back-ticks and interpolation.

    let myVaribale = 23; // Not Null
    const htmlTest = `<div>
          <span>Testing the line-break</span>
          ${myVaribale && `<br/>` }
          <p> Life is a test</p>
    </div>`;

document.body.innerHTML = htmlTest;
<html>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!