I sometimes have to write code in JavaScript that handles both null
and undefined
the same way. For example, a function that removes from an array every item that is either null
or undefined
. How should I name the function?
removeNullAndUndefined
is very long. removeNull
is short but imprecise. Ideally I want to name it removeX
, where X
has the meaning "null or undefined".
Does anyone know a good word for X
?
undefined and null variables oftentimes go hand-in-hand, and some use the terms interchangeably. Though, there is a difference between them: undefined is a variable that refers to something that doesn't exist, and the variable isn't defined to be anything. null is a variable that is defined but is missing a value.
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.
Both undefined and null are falsy by default. So == returns true. But when we use the strict equality operator (===) which checks both type and value, since undefined and null are of different types (from the typeof Operator section), the strict equality operator returns false.
In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value. You can assign it to a variable.
Yes: Nullish. For instance, in the current Stage 3 Nullish Coalescing Operator proposal.
So: removeNullish
or similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With