NOT NOT (!!)
(Source/Credits: https://dev.to/tyronasaurus_dev/not-not-gbm)
Picture this You need to check if an element exists on a page, let's take a look at this e...
Picture this
You need to check if an element exists on a page, let's take a look at this example:
You have a list of cool bugs on your website, but you want to check if the ladybug element is present on a specific instance (maybe the user initially chooses a bunch of insects from a list)
js
function doesLadybugExist() {
return (document.getElementById("lady-bug"));
// This will either return the element, or null
}
This looks alright... but, this will either return the element, or it will return null, which is not wrong, but in this example its not desired, we want to see if the ladybug exists
Lady bugs are !! cool
js
function doesLadybugExist() {
return (!!document.getElementById("lady-bug"));
// This will either true if it exists, or false
}
Take a closer look and see the !!
This is great because it returns a boolean value for us to if the user likes ladybugs or not. Success!
But not so fast. How does this work?
A quick google search will tell you that !! does not exist in a java context. It is simply the ! operator used twice.
What this does is it converts a non-boolean to an inverted boolean, meaning any truthy values get evaluated to true, and any falsy values get evaluated to false (in a boolean context)
All values are truthy unless they are defined as falsy
Below are the values that are falsy in Javascript according to MDN
No. | Value ---|--- 1. | false 2. | null 3. | undefined 4. | 0 (note: the string '0' evaluates to true) 5. | NaN 6. | '' 7. | document.all
Using !! is a clean, and easy way of comparing the state of an element on your DOM against a boolean value without having to worry about type-casting and other conversion methods.
And, lastly, do not not leave a like and a unicorn ;)
Comments section
adam_cyclones
•May 1, 2024
100% not a good plan. Values are already truethy or falsey. Narrowing your data down to booleans using coercion is wasted effort and a practice which I suppose avoids side effects with no actual gain. I can appreciate that it's easier to comprehend that true is true and false is false, but knowing what is falsey and what is truethy in JavaScript at a language level is far more valuable.
I appreciate your passion for the idea and I see that you want clean code which is commendable, I know that I once fell for such seductive techniques. But my mentor at the time taught me the value of clean code Vs understandable code and if you have to think about anything not not being what it is then it's not not not good.
juyn
•May 1, 2024
We have the same behavior in PHP. I really dislike this syntax.
It's unclear and you have to think too much in order to get what the developer meant.
Also, KISS ! It should be simple, and if you want a Boolean, simply cast it to a Boolean using the dedicated methods
maruru
•May 1, 2024
Your code has one bracket too many in the return statement in both code snippets.
tyronasaurus_dev Author
•May 1, 2024
Thanks i edited the post!
thematrixan
•May 1, 2024
Yes, it will work but ESLint will mark it as redundant negation. IMHO it is better to use
Boolean(...)
.maruru
•May 1, 2024
ESLint is opinionated and only as good as its configuration...