0

var result='16'>'141';
console.log(result);

var result='16'>141;
console.log(result);

That’s because if any of the operands is not string, then both operands become numbers, and the comparison becomes correct.

Can anyone tell me. How below equation is evaluate?

var result='a'>11;
console.log(result);

'a'>11=> Answer should be true instead of false;

because 'a' will convert to int 97 > 11 => true then how it evaluate false. If I go like this 'a'>'11' => then it answer comes true.

var result='a'>'11';
    console.log(result);

1
  • 4
    "'a' will convert to int 97" NO Number('a') will be NaN and NaN is never equals to anything including itself.. Commented Jun 27, 2016 at 18:09

2 Answers 2

5

When you convert a non-numeric string like 'a' to a Number, you get NaN:

console.log(+'a');        // NaN
console.log('a' * 1);     // NaN
console.log(Number('a')); // NaN

And NaN always produces false in relational comparisons. Trichotomy does not hold:

console.log(NaN < 0);  // false
console.log(NaN > 0);  // false
console.log(NaN == 0); // false

If you want to convert 'a' to 97, use charCodeAt:

console.log('a'.charCodeAt(0)); // 97

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

1 Comment

Few of the readers may not know that Unary plus acts as Number()
0

Javscript use The Abstract Equality Comparison Algorithm

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

When comparing a string and a number, string is converted to a number, but you are thinking like some ASCII code.

Since 'a' is not a number so it's comparison with a number will give you false in any case > , < or ==.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.