0

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?

user3840170
  • 22,750
  • 2
  • 21
  • 50
Sayan
  • 109
  • 2
  • 1
    May want to take look at this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness – Timur May 21 '22 at 20:00

0 Answers0