path | title |
---|---|
/learnings/javascript_lodash |
Learnings: Javascript: Lodash |
Lodash: returns the variable OR the default value
someVar ?? "a default"
Lodash: checks to see if a value is null or undefined
!!!b
!! returns if a value is convertable to a boolean type. Which is great - because even false
can be converted.
But we want to see values that can't be - undefined
and null
to be exact. So get the !
version of this.
Yes this feels weird - I don't think I like it.
I liked lodash's iteration methods because they took care of null variables. Oh well...
let theResult = (myList ?? []).collect( (f) => f+1 )
optional chaining short circuits the statement when null is encountered
javascript
let myList = null
myList?.collect( (f) => f+1) // will not throw method not found error b/c short circuited
If you want the result to always be something, like theResult
above, you could:
javascript
let myList = null
let myResult = myList?.collect( (f) => f+1) ?? []