0
var num1 = prompt("Enter first number: ", "number");
var num2 = prompt("Enter second number: ", "number");

if (num1 > num2) {

    document.write("<p>" + num1 + " is greater than " + num2 + "</p>");
} else {

    document.write("<p>" + num2 + " is greater than " + num1 + "</p>");
}

When I execute this code at that time it executes else part everytime so what is solution for it? Thank you so much in advance.

1
  • you are comparing two stings Commented May 21, 2013 at 23:03

6 Answers 6

8

You are comparing strings (which is what prompt returns, even if the user types in digits). Convert the values to numbers first:

if(+num1 > +num2){

If the user enters any non-digits into the prompts, a NaN will be produced and you may see weird results. I would set it up this way:

var num1 = +prompt("Enter first number: ", "number"),
    num2 = +prompt("Enter second number: ", "number");

if (!isNan(num1) && !isNaN(num2)) {
    if (num1 > num2) {

    } else if (num2 > num1) {

    } else {
        // Numbers equal
    }
} else {
    // At least one of them isn't a number
}
Sign up to request clarification or add additional context in comments.

9 Comments

Is this a JS shortcut I don't know about that gets around the parse functions? If so, interesting.
I'm just wondering what + actually does, I've never seen it in JS before. Is it a prefix operator that casts to the widest available numeric value? Anybody have any docs on this cool little thing?
@TreyE It's basically an alias for Number(x) - it's the unary plus operator
@MichaelTodd They're not identical, +" " is 0 for example where parseInt(" ") is NaN
nice trick to coerce string into number, but if user does not enter digits comparison will be erratic
|
2

Javascript has a funky definition of true/false when you compare arbitrary variables. You are going to want to make 100% sure that the input of the two parameters you are prompting for has been cast to a number like you would expect it to be:

var num1 = parseInt(prompt("Enter first number: ", "number"));
var num2 = parseInt(prompt("Enter second number: ", "number"));

Using the + operator is great shorthand, but aside from my quick answer above you are probably going to want to add some checking to make sure they aren't giving you garbage.

Also, be sure to check out Ian's answer - it contains a great explanation of the + operator and how it can be used for coercion.

2 Comments

Thank you so much for your help. Actually I think that there is no any difference of data type in JAVASCRIPT like in JAVA. But thank you so much for your kind and useful information....
@RajPatel No difference in data type? Then what does G + T equal? Of course there's a difference in data type. Automatic type conversion does not mean that there's no such thing as a data type.
1

You need to parse the input (a string) into a number (an integer/float) before comparing.

Use parseInt or parseFloat like: (respectively)

var num1 = parseInt(prompt("Enter first number: ", "number"));

var num2 = parseFloat(prompt("Enter second number: ", "number"));

Comments

0

prompt returns a string, not number. You'll need to convert the results of prompt to a number before comparing.

Comments

0
var num1 = parseFloat(prompt("Enter first number: ", "number"));
var num2 = parseFloat(prompt("Enter second number: ", "number"));

prompt returns a String value, which needs to be parsed to a number.

Comments

0

In your case variables num1 and num2 are STRING. Before compare need to convert to INT type:

var num1 = parseInt(prompt("Enter first number: ", "number"));

var num2 = parseInt(prompt("Enter second number: ", "number"));

Tip on future: you can will get type of your's variable by typeof()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.