Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course:
The "fromNullable" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that fromNullable is useful because it is a definitive null check for every function, and avoids repetition.

Get Unlimited Access Now

Transcript from the "fromNullable" Lesson

>> Before we go into the next example, we've got fromNullable. Very helpful utility. So we'll say I take some value x and if x is not equal to null, I'll go ahead and return a right of the x, rather return to the left of nothing. And then we can take this big nasty, I don't like to use curly braces and return if you've noticed.

[00:00:27] So we can actually just call fromNullable on this thing. I know this is, bear with me and, whoops. And so what's happening here is it's basically saying, if you can, it's a little squished. But the idea is that fromNullable captures, hey, if whatever's in here is null just return it a right or a left based on if it's null or not.

[00:00:56] That way I don't have to keep writing, assigning variables and then checking that. I have one definitive null check that I can return you if my function ever returns null I'll do it in the function, so then my function becomes much safer for every caller of that function.

[00:01:15] Cool, so we have that. And now let's blow that away. Did anybody see that a little longer? I wanna take that away, you'll see it, pretty good. You don't have to implement these. Either box all this stuff is there's libraries out there for you and they're functional programming Monad libraries out there.

[00:01:37] There's predefined a lot of these types.