Em Sta
Em Sta

Reputation: 1696

Javascript,doesn't show undefined number

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+""; 
}

Upvotes: 3

Views: 101

Answers (3)

Teemu
Teemu

Reputation: 23396

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

if (isNaN(neuezahl)) {...}

Upvotes: 2

user2082189
user2082189

Reputation: 13

the condition in the if statement may not correct. Now you use "=" not "==", it is an assignment statement and the condition will always true. So if you want to check "neuezahl" is "NaN", function isNaN may help.

if (isNaN(neuezahl)){...}
else {}

Upvotes: 0

Darren
Darren

Reputation: 70718

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

Upvotes: 5

Related Questions