The lodash method isNil exported as a Node.js module for checking null or undefined values
npx @tessl/cli install tessl/npm-lodash--is-nil@4.0.0lodash.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.
npm install lodash.isnilvar isNil = require('lodash.isnil');For ES6 modules (with transpilation):
import isNil from 'lodash.isnil';var isNil = require('lodash.isnil');
// Check for null or undefined values
console.log(isNil(null)); // => true
console.log(isNil(undefined)); // => true
console.log(isNil(void 0)); // => true
// All other values return false
console.log(isNil(NaN)); // => false
console.log(isNil('')); // => false
console.log(isNil(0)); // => false
console.log(isNil(false)); // => false
console.log(isNil([])); // => false
console.log(isNil({})); // => falseThe isNil function provides reliable checking for nullish values (null or undefined) across different JavaScript environments.
/**
* Checks if value is null or undefined.
*
* @param {*} value The value to check.
* @returns {boolean} Returns true if value is nullish, else false.
*/
function isNil(value);Implementation Details:
value == nullnull and undefined values with a single comparisonUsage Examples:
var isNil = require('lodash.isnil');
// Basic nullish checking
if (isNil(someVariable)) {
console.log('Variable is null or undefined');
}
// Function parameter validation
function processData(data) {
if (isNil(data)) {
throw new Error('Data parameter is required');
}
// Process data...
}
// Array filtering
var values = [1, null, 'hello', undefined, 0, ''];
var nonNullish = values.filter(function(value) {
return !isNil(value);
});
// Result: [1, 'hello', 0, '']
// Object property checking
var user = { name: 'John', age: null, email: undefined };
Object.keys(user).forEach(function(key) {
if (isNil(user[key])) {
console.log(key + ' has no value');
}
});Comparison with Native JavaScript:
var isNil = require('lodash.isnil');
var value = null;
// lodash.isnil approach (recommended)
isNil(value); // => true
// Native JavaScript alternatives
value == null; // => true (same as isNil implementation)
value === null; // => true (only for null, not undefined)
value === undefined; // => false (only for undefined, not null)
typeof value === 'undefined' || value === null; // => true (verbose)Error Handling:
The isNil function never throws errors and can safely handle any input type, including: