DEV Community

Cristian Fernando
Cristian Fernando

Posted on

3 1

Paracetamol.js💊| #97: Explica este código JavaScript

Explica este código JavaScript

const toBolean = x => Boolean(x);

console.log(toBolean(37));
console.log(toBolean(0/0));
console.log(toBolean(0));
console.log(toBolean({}));
console.log(toBolean(Symbol("Soy un symbol")));
Enter fullscreen mode Exit fullscreen mode

A. true, false, false, true, true
B. false, false, true, true, false
C. true, true, false, false, false
D. false, ReferenceError, false, false, true

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:
A. true, false, false, true, true

El constructor Boolean permite convertir valores a tipo boolean.

Los valores truthy como el número 37, un objeto vacío, o un Symbol infieren a true sin ninguna complicación.

Valores como NaN, cadenas vacías o 0 al ser considerados valores falsy inferirán a false.

A continuación una tabla que resume todas las posibles conversiones a boolean:

x Boolean(x)
undefined false
null false
true o false Sin cambios
number 0 => false, NaN => false
Cualquier otro number => true
bigint 0n => false
Cualquier otro bigint => true
string "", '', => false
Cualquier otro string => true
symbol true
object Siempre true

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or leave a kind comment!

Okay