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

318
Views
Convertir verdadero o falso en un booleano explícito, es decir, en Verdadero o Falso

tengo una variable Llamémoslo toto .

Este toto se puede establecer en undefined , null , una cadena o un objeto.

Me gustaría verificar si toto se establece en datos, lo que significa establecer en una cadena o un objeto, y ni undefined ni null , y establecer el valor booleano correspondiente en otra variable.

¡Pensé en la sintaxis !! , eso se vería así:

 var tata = !!toto; // tata would be set to true or false, whatever toto is.

¡El primero ! se establecería en false si toto no está undefined o es null y true en caso contrario, y el segundo lo invertiría.

Pero se ve un poco raro. Entonces, ¿hay una manera más clara de hacer esto?

Ya miré esta pregunta , pero quiero establecer un valor en una variable, no solo verificarlo en una declaración if .

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Sí, siempre puedes usar esto:

 var tata = Boolean(toto);

Y aquí algunas pruebas:

 for (var value of [0, 1, -1, "0", "1", "cat", true, false, undefined, null]) { console.log(`Boolean(${typeof value} ${value}) is ${Boolean(value)}`); }

Resultados:

 Boolean(number 0) is false Boolean(number 1) is true Boolean(number -1) is true Boolean(string 0) is true Boolean(string 1) is true Boolean(string cat) is true Boolean(boolean true) is true Boolean(boolean false) is false Boolean(undefined undefined) is false Boolean(object null) is false
over 4 years ago · Santiago Trujillo Report

0

!!o también es una abreviatura de Boolean(o) y funciona exactamente igual. (para convertir verdadero/ truthy/falsy en true/false ).

 let o = {a: 1} Boolean(o) // true !!o // true // !!o is shorthand of Boolean(o) for converting `truthy/falsy` to `true/false`

Tenga en cuenta que

over 4 years ago · Santiago Trujillo 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!