Is there a JavaScript idiom for changing "undefined" to "null"?

There are a lot of JavaScript idioms that force types and similar things to each other.

! 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 to an actual number, etc.

Is there something similar that converts undefined to null ?

Now am I using ternary ? : ? : but it would be nice to know if I have a useful trick.


Ok, let me come up with an example ...

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

callback is called by third-party code, which sometimes passes undefined .
Third-party code can pass null , but not undefined . format() also a third party and cannot handle a transmission of either undefined or null .

+5
source share
1 answer

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

+2
source

Source: https://habr.com/ru/post/1271625/


All Articles