javascript-lodashHow can I check if a variable is null or undefined using Lodash in JavaScript?
To check if a variable is null or undefined using Lodash in JavaScript, you can use the _.isNil() method. This method returns true if the given value is null or undefined.
Example code
const _ = require('lodash');
let a = null;
let b = undefined;
let c = 0;
console.log(_.isNil(a)); // true
console.log(_.isNil(b)); // true
console.log(_.isNil(c)); // false
Output example
true
true
false
The .isNil() method takes in one argument, the value to be tested. In the example above, the values of a
and b
are null and undefined respectively. The .isNil() method returns true for both of these values since they are both null or undefined. The value of c
is 0, so the _.isNil() method returns false.
Helpful links
More of Javascript Lodash
- How can I use Lodash to create a unique array in JavaScript?
- How can I use lodash in a JavaScript sandbox?
- How can I use Lodash to union two JavaScript arrays?
- How do I use Lodash to zip two JavaScript arrays together?
- How do I use Lodash in a JavaScript playground?
- How do I use Lodash to truncate a string in JavaScript?
- How do I use Lodash to sort an array of objects by a specific property in JavaScript?
- How to resolve a "_ is not defined" error when using Lodash in JavaScript?
- How can I use Lodash to remove a nested property from an object in JavaScript?
- How can I use Lodash to uppercase the first letter of a string in JavaScript?
See more codes...