9/14/2023 0 Comments Javascript check undefined![]() It is not immediately obvious what this code does for someone who is not familiar with this approach. The downside? It does not contain the word undefined. It is even three characters shorter than undefined. It can not be overridden, can not be shadowed, and does not produce any false positives. If you look into some libraries, they use this approach. To check for undefined that works every time, compare with void 0: let variable Ĭonsole.log(variable = void 0) // true ![]() So don't rush and refactor everything just yet, but let it be a cautionary tale how messed up a seemingly simple everyday problem can be. ![]() I didn't think much about it, but when I started researching this topic, I was surprised by the number of approaches to achieve this simple goal, and that most of them are broken in subtle ways.įortunately, most ways are broken only if you deliberately try to break them. To check if a variable is undefined is so commonplace I hardly write any programs without it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |