admin管理员组文章数量:1289583
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
?
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
?
- 1 Depends on context. "Empty" is the general term, but if it can be a string, "empty" may also mean "a string with no characters in it", which may or may not be desirable. – Dave Newton Commented Aug 1, 2019 at 15:11
- So you are asking a good name for a function that would remove null and undefined value without using the term removeNullAndUndefined (I am totally fine with this by the way). I would go with : removeFalsyValues. but at this point there isn't a "good word" you have to made it up so it's kind of up to you I guess – MaieonBrix Commented Aug 1, 2019 at 15:13
-
@MaieonBrix - But
0
,""
,NaN
, andfalse
are also falsy, and the OP seems to want to leave them alone. There is a word for this: "nullish." See my answer. – T.J. Crowder Commented Aug 1, 2019 at 15:14 - The actual subject is covered here stackoverflow./a/50211024/295783 – mplungjan Commented Aug 1, 2019 at 15:15
- 1 Not sure how this question can have an answer since it is really personal opinion. – epascarello Commented Aug 1, 2019 at 15:20
2 Answers
Reset to default 13Yes: Nullish. For instance, in the current Stage 3 Nullish Coalescing Operator proposal.
So: removeNullish
or similar.
When I need to check for null
or undefined
I often name such function either isNil
or isNothing
e.g.
const isNil = thing => thing === null || thing === undefined;
If you must adopt a naming convention I'd opt for what's quite mon to see in functional programming, namely the Maybe monad which es with a subtype to represent nothing: Nothing
or None
.
If you look at monetjs:
The Maybe type is the most mon way of representing nothingness (or the null type) with making the possibilities of NullPointer issues disappear.
Maybe is effectively abstract and has two concrete subtypes: Some (also Just) and None (also Nothing).
Under the hood, it uses a isNothing
function for checking for null
or undefined
In ramda.js, such function is called isNil
:
Checks if the input value is null or undefined
(Lodash has a similar method with the exact same name.)
本文标签: javascriptIs there a word that covers both Null and UndefinedStack Overflow
版权声明:本文标题:javascript - Is there a word that covers both Null and Undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741427874a2378183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论