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

Kenny
  • 5,407
  • 4
  • 19
  • 37
0

You can Use isNAN

var x = '23b';
var y = '23a';
if(isNAN(x) && isNAN(y))
alert("both are NAN");
Afnan Ahmad
  • 2,426
  • 3
  • 22
  • 41
0

var x = '23b';
var y = '23a';
console.log(parseFloat(x)===parseFloat(y));
Mr.Bruno
  • 4,519
  • 2
  • 11
  • 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,131
  • 4
  • 18
  • 30
Anant
  • 262
  • 6
  • 14