The NOT operator
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
The NOT operator (!) flips truthy and falsey values around. Truthy values becomes false while falsey values become true.
console.log(!22) // false
console.log(!false) // true
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)
}
You may notice that developers sometimes use two NOT operators together:
!!someVariable
!! here is called a double negation. It does the following:
truefalseIt 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.
What’s the result of each of these expressions?
!2550284!true!NaN!{}!!'Pandas are adorable!'!!''What’s the result of each of these expressions?
!2550284. false!true. false!NaN. true!{}. false!!'Pandas are adorable!'. true!!''. false