Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript,doesn't show undefined number

Tags:

javascript

I've created a variable with keys and values which looks like:

var e = new Array();
e[0] = "Bitte";
e[1] = "Danke";

Besides this I added a line in the variable which shows a text when the number is undefined.

e[NaN] = "Change Settings"; 

So when the variable e is NaN ("undefined"), I want that he doesn't displays the Number of the variable e in the input. I tried to achieve this as you can see, but it won't function.

if (neuezahl = NaN) {
  document.getElementById("saveServer").value="";
} else {
  document.getElementById("saveServer").value=""+neuezahl+""; 
}
like image 560
Em Sta Avatar asked Jan 21 '26 21:01

Em Sta


2 Answers

You have assigned neuzahl not compared it, aside that use the isNAN function:

if (isNAN(neuezahl))
{
   document.getElementById("saveServer").value="";
}
else 
{
   document.getElementById("saveServer").value=""+neuezahl+""; 
}

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/isNaN

like image 95
Darren Avatar answered Jan 24 '26 10:01

Darren


NaN can't be compared directly (it's not even equal to itself NaN === NaN ==> false). Use isNaN() to detect NaN:

if (isNaN(neuezahl)) {...}
like image 39
Teemu Avatar answered Jan 24 '26 10:01

Teemu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!