简体   繁体   中英

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

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

function isNil(value) {
  return value == null;
}

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM