0
# lodash.isnan
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash.isnan
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.isnan`
10
11
## Core Imports
12
13
```javascript
14
const isNaN = require('lodash.isnan');
15
```
16
17
For ES modules:
18
19
```javascript
20
import isNaN from 'lodash.isnan';
21
```
22
23
## Basic Usage
24
25
```javascript
26
const isNaN = require('lodash.isnan');
27
28
// Test with actual NaN values
29
console.log(isNaN(NaN)); // => true
30
console.log(isNaN(new Number(NaN))); // => true
31
32
// Test with non-NaN values that global isNaN incorrectly handles
33
console.log(isNaN(undefined)); // => false (global isNaN returns true!)
34
console.log(isNaN('hello')); // => false (global isNaN returns true!)
35
console.log(isNaN({})); // => false (global isNaN returns true!)
36
37
// Test with regular numbers
38
console.log(isNaN(42)); // => false
39
console.log(isNaN('42')); // => false
40
console.log(isNaN(Infinity)); // => false
41
console.log(isNaN(-Infinity)); // => false
42
```
43
44
## Capabilities
45
46
### NaN Detection
47
48
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.
49
50
```javascript { .api }
51
/**
52
* Checks if `value` is `NaN`.
53
*
54
* **Note:** This method is based on `Number.isNaN` and is not the same as
55
* global `isNaN` which returns `true` for `undefined` and other non-number values.
56
*
57
* @static
58
* @memberOf _
59
* @since 0.1.0
60
* @category Lang
61
* @param {*} value The value to check.
62
* @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
63
*/
64
function isNaN(value): boolean
65
```
66
67
**Key Features:**
68
- Based on `Number.isNaN` behavior for correct NaN detection
69
- Only returns `true` for actual NaN values (primitive or Number object)
70
- Returns `false` for `undefined`, strings, objects, and other non-number values
71
- Handles both primitive numbers and Number objects correctly
72
- Performs proper type checking before NaN comparison
73
74
**Usage Examples:**
75
76
```javascript
77
const isNaN = require('lodash.isnan');
78
79
// Correct NaN detection
80
isNaN(NaN); // => true
81
isNaN(new Number(NaN)); // => true
82
83
// Correctly identifies non-NaN values
84
isNaN(undefined); // => false
85
isNaN('string'); // => false
86
isNaN({}); // => false
87
isNaN([]); // => false
88
isNaN(null); // => false
89
90
// Numbers (including special numbers) are not NaN
91
isNaN(42); // => false
92
isNaN(0); // => false
93
isNaN(-42); // => false
94
isNaN(Infinity); // => false
95
isNaN(-Infinity); // => false
96
isNaN(Number.MAX_VALUE); // => false
97
isNaN(Number.MIN_VALUE); // => false
98
99
// Comparison with global isNaN
100
isNaN(undefined); // => false (lodash.isnan)
101
window.isNaN(undefined); // => true (global isNaN - incorrect!)
102
103
isNaN('123'); // => false (lodash.isnan)
104
window.isNaN('123'); // => false (global isNaN - happens to be correct)
105
106
isNaN('hello'); // => false (lodash.isnan)
107
window.isNaN('hello'); // => true (global isNaN - incorrect!)
108
```
109
110
## Implementation Details
111
112
The function uses a two-step process for reliable NaN detection:
113
114
1. **Type Check**: First verifies if the value is a number (primitive or Number object) using `typeof` check and `Object.prototype.toString`
115
2. **NaN Check**: Then uses the unique property of NaN that it's not equal to itself (`value != +value`)
116
117
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.
118
119
## Error Handling
120
121
This function does not throw any errors. It safely handles all JavaScript value types including:
122
- Primitives: `number`, `string`, `boolean`, `undefined`, `null`, `symbol`, `bigint`
123
- Objects: Plain objects, arrays, functions, dates, regular expressions
124
- Special values: `NaN`, `Infinity`, `-Infinity`
125
126
All input values are processed safely and return the appropriate boolean result.