DEV Community

Cristian Fernando
Cristian Fernando

Posted on • Edited on

1

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

Explica este código JavaScript

Dificultad: Intermedio

const fn = (x) => x !== NaN && x !== Infinity && x !== -Infinity

console.log(fn("3"))
console.log(Number.isFinite("3"))
Enter fullscreen mode Exit fullscreen mode
  • A. false, false
  • B. false, true
  • C. true, true
  • D. true, false

Respuesta en el primer comentario.

Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:

  • D. true, false

Number.isFinite() (así con el contructor Number) es la manera que tenemos en JavaScript para determinar si un número es real o no.

Para JavaScript un número será real siempre y cuando sea diferente de NaN, diferente de Infinity y diferente de -Infinity; como se ve en la función fn.

La diferencia es que en nuestra función fn JavaScript infiere por ejemplo la cadena "3" a 3, y por ello nos da true.

En cambio cuando usamos Number.isFinite() no hay inferencia de tipos, debemos pasarle un número para que haga bien su trabajo, por ello nos da false.

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay