DEV Community

Functional Javascript
Functional Javascript

Posted on • Edited on

3

Null vs Undefined? Answer: Nil

In JS I don't think in terms of null or undefined, but think in terms of "nil".
I use a utility func similar to Ramda's R.isNil

And I have a complement func called, isNotNil

/**
@func
true if var is null or undefined

@param {*} v
@return {boolean}
*/
export const isNil = v => v === undefined || v === null;
/**
@func complement

@param {*} v
@return {boolean}
*/
export const isNotNil = v => !isNil(v);
//@tests
const aTrue = [undefined, null, (() => undefined)(), (() => null)(), (() => console.log())()];
const aFalse = ["", 0, -0, [], {}, () => undefined, "undefined", "null", NaN, -Infinity, 9e9999, 9999n];
logForeachParam(isNil, aTrue);
logForeachParam(isNil, aFalse);

Result of test:
isNil test

logForeachParam sourcecode at:

https://gist.github.com/funfunction/42918a4751ae51828cfc4c2dd4c0678e

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more