B
B
bismoney2022-02-20 22:45:07
JavaScript
bismoney, 2022-02-20 22:45:07

js check isset?

Guys, tell me please.
Why is this type of verification bad?
https://habr.com/ru/sandbox/44911/
Found on Habré, more than 9 years have passed.
I tested it on a node js project, but I can't figure out why it's not being used?
Are there any pitfalls?
And how to make checks in node js shorter to check '',null,undefined?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2022-02-20
@bismoney

The operator !is boolean negation. And it casts its argument to boolean. The author did not discover America, and this is being used. But you need to use it wisely.
Values ​​cast to falseare undefined, null, ''(empty string), 0, 0n(bigint 0), NaN.
Accordingly, when we have, for example, a string or undefined, or, for example, a number or undefined, such a check for undefined can be unsafe, since it will also cut off an empty string, 0 and NaN.
!!is just 2 operators in a row !, it's such a popular shorthand for quick casting to boolean.
The author of the article also suggests using !!!, but this is pointless, since a triple negation will give the same thing as a single one.

P
profesor08, 2022-02-20
@profesor08

!!someVar- why do a type cast and invert it? Need to check a variable for undefined? Well, check it out. Need to know if something has been announced? Whether well so learn the type is undefined.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question