Skip to content

Latest commit

 

History

History
64 lines (40 loc) · 1.61 KB

learning_javascript_lodash.md

File metadata and controls

64 lines (40 loc) · 1.61 KB
path title
/learnings/javascript_lodash
Learnings: Javascript: Lodash

Table Of Contents

Lodash Utilities

How I'm replacing Lodash

defaultTo

Lodash: returns the variable OR the default value

someVar ?? "a default"

isNull

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.

Lodash's iteration methods

I liked lodash's iteration methods because they took care of null variables. Oh well...

let theResult = (myList ?? []).collect( (f) => f+1 )

replacing using optional chaining

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) ?? []