0
# lodash.isnil
1
2
lodash.isnil is a standalone Node.js module that provides the lodash method `isNil` for checking if a value is null or undefined (nullish values). This modularized approach allows developers to use just this specific utility without importing the entire lodash library, making it ideal for applications focused on bundle size optimization.
3
4
## Package Information
5
6
- **Package Name**: lodash.isnil
7
- **Package Type**: npm
8
- **Language**: JavaScript (ES5 compatible)
9
- **Installation**: `npm install lodash.isnil`
10
11
## Core Imports
12
13
```javascript
14
var isNil = require('lodash.isnil');
15
```
16
17
For ES6 modules (with transpilation):
18
19
```javascript
20
import isNil from 'lodash.isnil';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var isNil = require('lodash.isnil');
27
28
// Check for null or undefined values
29
console.log(isNil(null)); // => true
30
console.log(isNil(undefined)); // => true
31
console.log(isNil(void 0)); // => true
32
33
// All other values return false
34
console.log(isNil(NaN)); // => false
35
console.log(isNil('')); // => false
36
console.log(isNil(0)); // => false
37
console.log(isNil(false)); // => false
38
console.log(isNil([])); // => false
39
console.log(isNil({})); // => false
40
```
41
42
## Capabilities
43
44
### Nullish Value Checking
45
46
The `isNil` function provides reliable checking for nullish values (null or undefined) across different JavaScript environments.
47
48
```javascript { .api }
49
/**
50
* Checks if value is null or undefined.
51
*
52
* @param {*} value The value to check.
53
* @returns {boolean} Returns true if value is nullish, else false.
54
*/
55
function isNil(value);
56
```
57
58
**Implementation Details:**
59
- Uses loose equality check: `value == null`
60
- Catches both `null` and `undefined` values with a single comparison
61
- More reliable than strict equality checks in some edge cases
62
- Compatible with all JavaScript environments (ES5+)
63
64
**Usage Examples:**
65
66
```javascript
67
var isNil = require('lodash.isnil');
68
69
// Basic nullish checking
70
if (isNil(someVariable)) {
71
console.log('Variable is null or undefined');
72
}
73
74
// Function parameter validation
75
function processData(data) {
76
if (isNil(data)) {
77
throw new Error('Data parameter is required');
78
}
79
// Process data...
80
}
81
82
// Array filtering
83
var values = [1, null, 'hello', undefined, 0, ''];
84
var nonNullish = values.filter(function(value) {
85
return !isNil(value);
86
});
87
// Result: [1, 'hello', 0, '']
88
89
// Object property checking
90
var user = { name: 'John', age: null, email: undefined };
91
Object.keys(user).forEach(function(key) {
92
if (isNil(user[key])) {
93
console.log(key + ' has no value');
94
}
95
});
96
```
97
98
**Comparison with Native JavaScript:**
99
100
```javascript
101
var isNil = require('lodash.isnil');
102
103
var value = null;
104
105
// lodash.isnil approach (recommended)
106
isNil(value); // => true
107
108
// Native JavaScript alternatives
109
value == null; // => true (same as isNil implementation)
110
value === null; // => true (only for null, not undefined)
111
value === undefined; // => false (only for undefined, not null)
112
typeof value === 'undefined' || value === null; // => true (verbose)
113
```
114
115
**Error Handling:**
116
117
The `isNil` function never throws errors and can safely handle any input type, including:
118
- Primitive values (strings, numbers, booleans)
119
- Objects and arrays
120
- Functions
121
- Symbols (in ES6+ environments)
122
- Exotic objects and host objects