Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda