Created
March 12, 2015 19:31
-
-
Save kimmobrunfeldt/76e40021365daffa9bc9 to your computer and use it in GitHub Desktop.
One reason why !! is dangerous
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// _.find returns undefined if nothing is found, otherwise returns the found item. | |
var existsOnCurrentPeriod = _.find(currentValues.operators, function(operatorId) { | |
return operatorId === operator.usbKeyId; | |
}); | |
operator.existsOnPeriod = !!existsOnCurrentPeriod; | |
// This looks correct at first glance, but see below |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The same logic with slight variable name changes | |
var operatorIds = [0, 1, 2, 3]; | |
var usbKeyId = 0; | |
var existsOnCurrentPeriod = _.find(operatorIds, function(id) { | |
return id === usbKeyId; | |
}); | |
var exists = !!existsOnCurrentPeriod; | |
console.log(exists); | |
> false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var operatorIds = [0, 1, 2, 3]; | |
var usbKeyId = 0; | |
var existsOnCurrentPeriod = _.find(operatorIds, function(id) { | |
return id === usbKeyId; | |
}); | |
var exists = !_.isUndefined(existsOnCurrentPeriod); | |
console.log(exists); | |
> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment