Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda