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

165
Vistas
Can we simplified this code? show = typeof show == 'undefined' ? undefined : !show
function myToggle(show) {
    //...bunch of codes here...

    show = typeof show == 'undefined' ? undefined : !show
    $(el).toggleClass('d-none', show )
}

In above function, show will be inverted (boolean) only if its defined, then pass into jQuery's toggleClass(). d-none is a Bootstrap class to hide element.

So that when:

myToggle()    //this will toggle class d-none to the element
myToggle(1)   //this will remove class d-none from the element
myToggle(0)   //this will give class d-none to the element

So my question, can we simplified this code?

show = typeof show == 'undefined' ? undefined : !show
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Let's start with the fact that this should be ===, not just ==. You almost never want == in JS (it's not Java or C++, always use === unless you actually know why you shouldn't). With that, you don't need a typeof check: just check whether your var is undefined, directly.

show = show === undefined? show : !show;

However, this ignores how jQuery's .toggleClass() even works. It doesn't take "true or false or undefined" as second argument, it takes a boolean, and "show" is pretty explicit in its intention, so the real simplication is the most obvious one:

show = !!show;
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