Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does isNil method in Lodash use null instead of undefined?

Tags:

Why does isNil method in Lodash use null instead of undefined?

function isNil(value) {   return value == null; } 
like image 854
mohsinulhaq Avatar asked Dec 14 '17 12:12

mohsinulhaq


People also ask

Is null or undefined Lodash?

Lodash helps in working with arrays, strings, objects, numbers, etc. The _. isNil() method is used to check if the value is null or undefined. If the value is nullish then returns true otherwise it returns false.

How do you check undefined in Lodash?

isUndefined() method is used to find whether the given value is undefined or not. It returns True if the given value is undefined.

How do you remove undefined and null values from an object using Lodash?

To remove a null from an object with lodash, you can use the omitBy() function. If you want to remove both null and undefined , you can use . isNull or non-strict equality.

Is Lodash null?

Lodash helps in working with arrays, strings, objects, numbers, etc. The _. isNull() method is used to find whether the value of the object is null. If the value is null then returns true otherwise it returns false.


2 Answers

It makes no difference either way in terms of the logic using null or undefined as null == undefined == true, but using null instead of undefined would make the file size smaller by 5 bytes.

It's simply done to save a few bytes making the file smaller and faster to download from the server.

like image 55
George Avatar answered Oct 28 '22 05:10

George


To understand this a bit better, it's important to note that lodash is using == here instead of ===.

Take the following example:

console.log(null == undefined);    // true console.log(null === undefined);   // false 

By using == (double equals), lodash is utilizing type coercion where null and undefined will be coerced to falsy values. As a result, null == undefined is true.

However, if using === (triple equals), no coercion is enforced, which means the types must be identical, and as we know null is not identical to undefined. As a result, null === undefined is false.

like image 39
lux Avatar answered Oct 28 '22 05:10

lux