The NOT operator can be used to eliminate else statements, like this:
const str = ''
if (!str) {
// Only do something if string is empty (NOT truthy)
}
Double negation
You may notice that developers sometimes use two NOT operators together:
!!someVariable
!! here is called a double negation. It does the following:
converts a truthy value to true
converts a falsey value to false
It works this way: if the value is truthy, convert it to false with the first ! NOT operator; then, convert false to true again with the second ! NOT operator (and vice versa).
Double negation is used to explicitly cast a truthy or falsey value into a boolean (true or false). You’ll almost never need it.