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

145
Vistas
can a string be equal to a number

var luckyNumber = prompt('What is your lucky number?','');

if (luckyNumber == 7 ) {
    document.write("<p>Hey, 7 is my lucky number too!</p>");
} else if (luckyNumber == 13 || luckyNumber == 24) {
    document.write("<p>Wooh. " + luckyNumber + "? That's an unlucky number!</p>");
} else {
    document.write("<p>The number " + luckyNumber + " is lucky for you!</p>");
}

This is a simple Java Script program which allows to enter a number from the user using prompt() command and store that number in a variable called luckyNumber. As far as I know the prompt() function always returns a string version of whatever we type. For example , if I type 7 , the function should return '7'. Now since luckyNumber is storing a string, if (luckyNumber == 7 ) should always be false, and always should execute the else part only, because A String value and and a number can not be the same, even if we consider the string as ASCII number. But the code works fine. How? also in my book it's written that '2'==2 is a true condition. How is that even possible?

about 4 years ago · Juan Pablo Isaza
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