DEV Community

Functional Javascript
Functional Javascript

Posted on • Updated on

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

Top comments (0)