Skip to content

num == null judgment is not rigorous #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jweboy
Copy link

@jweboy jweboy commented Apr 9, 2018

I think that using == to determine that undefined is not logically rigorous should be replaced by === ==

@@ -18,7 +18,7 @@ module.exports = function arrayFirst(arr, num) {
}

var first = slice(arr, 0, isNumber(num) ? +num : 1);
if (+num === 1 || num == null) {
if (+num === 1 || num === undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more rigorous than num === undefined.

var undefined = 'foo';
var val;
console.log(val === undefined) // false
console.log(val == null) // true

This is because undefined can be a defined value, but num == null checks both typeof num === 'undefined' and num === null.

@jweboy
Copy link
Author

jweboy commented Apr 17, 2018

Then I think that excluding undefined and null is more explicit here, replacing == with ===.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants