CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-isnan

The lodash method `_.isNaN` exported as a module for reliable NaN detection.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

lodash.isnan

lodash.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.

Package Information

  • Package Name: lodash.isnan
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.isnan

Core Imports

const isNaN = require('lodash.isnan');

For ES modules:

import isNaN from 'lodash.isnan';

Basic Usage

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));              // => false

Capabilities

NaN Detection

Checks 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): boolean

Key Features:

  • Based on Number.isNaN behavior for correct NaN detection
  • Only returns true for actual NaN values (primitive or Number object)
  • Returns false for undefined, strings, objects, and other non-number values
  • Handles both primitive numbers and Number objects correctly
  • Performs proper type checking before NaN comparison

Usage 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!)

Implementation Details

The function uses a two-step process for reliable NaN detection:

  1. Type Check: First verifies if the value is a number (primitive or Number object) using typeof check and Object.prototype.toString
  2. NaN Check: Then uses the unique property of NaN that it's not equal to itself (value != +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.

Error Handling

This function does not throw any errors. It safely handles all JavaScript value types including:

  • Primitives: number, string, boolean, undefined, null, symbol, bigint
  • Objects: Plain objects, arrays, functions, dates, regular expressions
  • Special values: NaN, Infinity, -Infinity

All input values are processed safely and return the appropriate boolean result.

docs

index.md

tile.json