Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda