Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!