skip to content
Dev Journal

Not Operator

/ 1 min read

NOT operator ! inverts truthy and falsy values. Truthy values become false, and falsy values become true.

console.log(!22) // false
console.log(!false) // true

Use the NOT operator to avoid else statements:

const str = ""
if (!str) {
// Do something if the string is empty (not truthy)
}

Double Negation

Double negation !! converts values to boolean:

  • !!someVariable

It works by first converting a truthy value to false with !, then back to true with another !. It explicitly casts values to true or false.