Skip to content

Instantly share code, notes, and snippets.

@asbp
Created August 5, 2025 04:10
Show Gist options
  • Save asbp/f01494b68e6a4baedfe9b1987dce9555 to your computer and use it in GitHub Desktop.
Save asbp/f01494b68e6a4baedfe9b1987dce9555 to your computer and use it in GitHub Desktop.
reliable way to check if a value is a NaN or not
function isValueNaN(value) {
if(typeof value === "string") {
value = String(value).trim().length < 1 ? NaN : value;
}
// Coerce to number (same as global isNaN behavior)
const num = Number(value);
// NaN is the only value not equal to itself
return num !== num;
}
const test = [
"",
" ",
"1.23",
"123ABC",
12,
NaN,
];
for(const item of test) {
console.log(`Is "${item}" a NaN?: ${isValueNaN(item)}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment