Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a JavaScript idiom to change "undefined" to "null"?

Tags:

There's quite a few JavaScript idioms that coerce between types and similar things.

! can convert anything falsey to boolean true, !! can convert anything falsey to actual boolean false, + can convert true, false, or a string representing a number into an actual number, etc.

Is there something similar that converts undefined to null?

Now I'm using ternary ? : but it would be cool to know if I'm missing a useful trick.


OK, let me contrive an example ...

function callback(value) {   return value ? format(value) : null; } 

callback is called by 3rd party code which sometimes passes undefined.
The 3rd party code can handle null being passed back, but not undefined. format() is also 3rd party and can't handle being passed either undefined or null.

like image 563
hippietrail Avatar asked Sep 08 '17 02:09

hippietrail


People also ask

Is undefined equal to null in JavaScript?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

How do you handle undefined and null in JavaScript?

The typeof operator for undefined value returns undefined . Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator. Note: We cannot use the typeof operator for null as it returns object .

How do you express null in JavaScript?

In JavaScript, null is a special value that represents an empty or unknown value. For example, let number = null; The code above suggests that the number variable is empty at the moment and may have a value later.

Why null == undefined is true in JavaScript?

null and undefined both return false . That's why your code is actually checking if false is equal to false . However their types are not equal. Because of that, the next statement will return false, as the === comparison operator checks both the types and their value.


2 Answers

undefined || null - or any falsey || null - will return null

like image 76
Hugo Silva Avatar answered Sep 20 '22 03:09

Hugo Silva


Javascript now supports a null-coalescing operator: ??. It may not be production-ready (consult the support table), but it's certainly safe to use with Node or a transpiler (TypeScript, Babel, etc.).

Per MDN,

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

Much as || can provide a "default" value when the left operand is falsey, ?? provides a "default" value if the left operand is null or undefined. You can use this to coerce undefined to null:

    // OR operator can coerce 'defined' values     "value"   || null;    // "value"     0         || null;    // null     false     || null;    // null     ""        || null;    // null     undefined || null;    // null          // The null-coalescing operator will only coerce undefined or null     "value"   ?? null;    // "value"     0         ?? null;    // 0     false     ?? null;    // false     ""        ?? null;    // ""     undefined ?? null;    // null 

An example based on the question:

    function mustNotReturnUndefined(mightBeUndefined) { // can return null         // Substitute empty string for null or undefined         let result = processValue(mightBeUndefined ?? "");          // Substitute null for undefined         return result ?? null;     } 
like image 37
snarf Avatar answered Sep 20 '22 03:09

snarf