The lodash method `_.isNaN` exported as a module for reliable NaN detection.
npx @tessl/cli install tessl/npm-lodash-isnan@3.0.0lodash.isnan provides a reliable NaN (Not a Number) detection function that implements the same behavior as Number.isNaN. Unlike the global isNaN function which incorrectly returns true for undefined and other non-number values, this implementation only returns true for actual NaN values, providing consistent and correct NaN detection.
npm install lodash.isnanconst isNaN = require('lodash.isnan');For ES modules:
import isNaN from 'lodash.isnan';const isNaN = require('lodash.isnan');
// Test with actual NaN values
console.log(isNaN(NaN)); // => true
console.log(isNaN(new Number(NaN))); // => true
// Test with non-NaN values that global isNaN incorrectly handles
console.log(isNaN(undefined)); // => false (global isNaN returns true!)
console.log(isNaN('hello')); // => false (global isNaN returns true!)
console.log(isNaN({})); // => false (global isNaN returns true!)
// Test with regular numbers
console.log(isNaN(42)); // => false
console.log(isNaN('42')); // => false
console.log(isNaN(Infinity)); // => false
console.log(isNaN(-Infinity)); // => falseChecks if a value is NaN using the same logic as Number.isNaN, providing reliable NaN detection that doesn't have the issues of the global isNaN function.
/**
* Checks if `value` is `NaN`.
*
* **Note:** This method is based on `Number.isNaN` and is not the same as
* global `isNaN` which returns `true` for `undefined` and other non-number values.
*
* @static
* @memberOf _
* @since 0.1.0
* @category Lang
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
*/
function isNaN(value): booleanKey Features:
Number.isNaN behavior for correct NaN detectiontrue for actual NaN values (primitive or Number object)false for undefined, strings, objects, and other non-number valuesUsage Examples:
const isNaN = require('lodash.isnan');
// Correct NaN detection
isNaN(NaN); // => true
isNaN(new Number(NaN)); // => true
// Correctly identifies non-NaN values
isNaN(undefined); // => false
isNaN('string'); // => false
isNaN({}); // => false
isNaN([]); // => false
isNaN(null); // => false
// Numbers (including special numbers) are not NaN
isNaN(42); // => false
isNaN(0); // => false
isNaN(-42); // => false
isNaN(Infinity); // => false
isNaN(-Infinity); // => false
isNaN(Number.MAX_VALUE); // => false
isNaN(Number.MIN_VALUE); // => false
// Comparison with global isNaN
isNaN(undefined); // => false (lodash.isnan)
window.isNaN(undefined); // => true (global isNaN - incorrect!)
isNaN('123'); // => false (lodash.isnan)
window.isNaN('123'); // => false (global isNaN - happens to be correct)
isNaN('hello'); // => false (lodash.isnan)
window.isNaN('hello'); // => true (global isNaN - incorrect!)The function uses a two-step process for reliable NaN detection:
typeof check and Object.prototype.toStringvalue != +value)This approach ensures that only actual number values are tested for NaN, avoiding the false positives that occur with the global isNaN function when it attempts to coerce non-numbers to numbers before testing.
This function does not throw any errors. It safely handles all JavaScript value types including:
number, string, boolean, undefined, null, symbol, bigintNaN, Infinity, -InfinityAll input values are processed safely and return the appropriate boolean result.