1

I am practicing the comparison operators in an if/else statement. My question is that I am expecting the window will give me a True alert, but instead it gave me the False alert. I thought the system will coerce num1 into 97 before it performs the comparison.

Here is my code:

var num1 = "a";
var num2 = 99999;

if (num1 <= num2) {
   window.alert("True");
}else {
   window.alert("False");
}
3
  • 3
    Why do you believe the system will count a as number 97? Commented Jan 19, 2018 at 1:59
  • num1 is a string, so 99999 will be converted to string too. String comparison will be performed. Commented Jan 19, 2018 at 2:02
  • 1
    Your assumptions about the nature of num1 is wrong. JavaScript does not support characters as a primitive type and therefore in your code it is trying to coerce a string to number, returning NaN. Commented Jan 19, 2018 at 2:03

4 Answers 4

1

I believe the answer to your question has to do with implicit conversions which are happening here. Consider the following line of code:

console.log(99999 + "a")

This will output 99999a, and it won't convert the "a" string to a number. Assuming the same happens with your code snippet, it explains the observations. What we are seeing is consistent with this:

var num1 = "a";
var num2 = "99999";

if (num1 <= num2) {
    console.log("True");
} else {
    console.log("False");
}

The reason why this is false is that the letter a is lexicographically greater than any single digit character. And since JavaScript is comparing two strings, they are sorting as text, and the first character is all which needs to be examined to render an ordering.

Sign up to request clarification or add additional context in comments.

3 Comments

If someone has a link to JavaScript documentation for this, feel free to drop a comment or edit the answer.
Hi Tim, Thanks for your answer. However, you have changed my var num2 into a string. With both num1 and num2 are both strings, the comparison will result in "False". My understanding type coercion is that the lowercase letter is greater than an uppercase letter, and an uppercase letter is greater than a number. In this case, the single letter is greater than the digit number.
@VivianP This is precisely what my answer says. Did you read my answer before commenting?
0

You can use .codePointAt() to get the code point of the charcter

var num1 = "a";
var num2 = 99999;

if (num1.codePointAt(0) <= num2) {
   window.alert("True");
} else {
   window.alert("False");
}

1 Comment

Hi, Thanks for your input. I have not use the .codePointAt() function yet, but will definitely try it out.
0

the above condition is translated as

    if(parseInt(num1)<= parseInt(num2)) {
      //something
    }

parseInt(num1) returns NaN (Not a number) and NaN is, by definition, not less than, equal to, or greater than any other number. Hence the output is false.

1 Comment

Thanks, Amaresh. This make senses to me.
0

num1="a" is a string and in the comparison num1<=num2, num2 is a number. Implicit conversion happens here and num2 is converted into a string. There are several ways to convert a string(of type "345", etc) into a number:

Number()
parseInt()
parseFloat()
bitwise ~~

But the string num1 contains a character so these functions return NaN(Not a number).Hence we cannot convert the string of characters into a number.

Your purpose can be achieved using the charCodeAt() function as follows :

var num1="a";
var num2=9999;
if(num1.charCodeAt(0)<=num2){
window.alert("True");
}else{
window.alert("False");
}

2 Comments

Will the type coercion be used in my original code and apply the special rule? I read it somewhere that if one operand is a number, then it will attempt to coerce the other operand into a number and perform the comparison. Or because my case is that num1 contains a character, so it will convert num2 into a string for the comparision?
The problem here is that it's unable to coerce "a" to a numeric value.. If num1="97" then it would have behaved the way you want it to behave.. Here num1 will be coerced to a number and then comparison will be performed. For example: num1="97"; num2=999; num1<num2; Here num1<num2 will give true because num1 is coerced to a number. But in your case num1="a" , and its not able to coerce character value to a number.

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.