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

144
Vistas
js types of Bool conversion

Do all 3 ways use the same conversion to bool?

function check(variable){
    let b1 = Boolean(variable);
    let b2 = !!variable;
    let b3 = variable ? true : false;
    
    return b1 === b2 && b2 === b3;
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Do all 3 ways use the same conversion to bool?

Yes. The Boolean function, the logical NOT operator and the conditional operator all convert the value to a boolean value via the internal ToBoolean algorithm:

enter image description here

(of course engines are free to implement it however they want, but it has to behave like the spec dictates)

about 4 years ago · Juan Pablo Isaza Denunciar

0

MDN Web Docs tell us the following:

Do not use a Boolean object to convert a non-boolean value to a boolean value. To perform this task, instead, use Boolean as a function, or a double NOT operator:

const x = Boolean(expression);     // use this...
const x = !!(expression);          // ...or this
const x = new Boolean(expression); // don't use this!

According to the docs, the first form is equivalent to the second form and looking at the second form, you could deduce that it is equivalent to the third form you suggested.

So all three of your ways should be equivalent and they are dependent on what JavaScript considers as a truthy value

As to why you shouldn't use new Boolean() to convert something to boolean, this section offers a bit of an explanation. TL;DR You might accidentally initialize non-truthy values as truthy.

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