Seth Seth - 3 years ago 116
Javascript Question

How do you check if something is NaN and it is not equal to a string?

I'm trying out this code and it's not working please help!

Here's the code:

var test = "3";

function printIsNaN(value) {
var bool = document.getElementById("booleanValue");
if (isNaN(value) === true) {
bool.style.color = "green";
bool.innerHTML = "true";
} else if (isNaN(value) === false) {
bool.style.color = "red";
bool.innerHTML = "false";
}
}

printIsNaN(test);


<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Stackoverflow Question</title>
<script src="something.js"></script>
</head>
<body>
<!-- START OF THE DOCUMENT -->
<span id="booleanValue"></span>
</body>
</html>




Thanks if you can help PLEASE!!!!!!!!

Answer Source

you could use ParseInt() to change the string into an integer

    function printIsNaN(value) {
  var bool = document.getElementById("booleanValue");
  if (isNaN(parseInt(value))) {
    console.log("nope");
  } else if (!isNaN(parseInt(value))) {
    console.log("yep");
  }
}

hope this helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download