1

I have two variables;

var x = '23b';
var y = '23a';

Now i have a logic which will compare, if they're equal i want to perform something

Note: Both when compared, if both are NaN still they should pass the condition

i have tried using this

if (Number(x) == Number(y)) 

This returns false even though both are NaN

Ram Df
  • 43
  • 1
  • 4
  • Possible duplicate of [Comparing NaN values for equality in Javascript](http://stackoverflow.com/questions/8965364/comparing-nan-values-for-equality-in-javascript) – codersl Jan 19 '17 at 09:52

4 Answers4

0

Why Number?

just use parseInt

if (parseInt(x, 10) == parseInt(y, 10)) 

The behaviour on NaN is misleading

just console this NaN == NaN

You will get value as false

Kiran Shinde
  • 5,732
  • 4
  • 24
  • 41
0

You can Use isNAN

var x = '23b';
var y = '23a';
if(isNAN(x) && isNAN(y))
alert("both are NAN");
Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44
0

var x = '23b';
var y = '23a';
console.log(parseFloat(x)===parseFloat(y));
LellisMoon
  • 4,810
  • 2
  • 12
  • 24
0

Try using Object.is()

var x = '23b';

var y = '23a';

Object.is(Number(x),Number(y)); => true

Answered here as well.. https://stackoverflow.com/a/48300450/617797

Lotus91
  • 1,127
  • 4
  • 18
  • 31
Anant
  • 342
  • 6
  • 14