True and False Values II


I am stuck on understanding why the console will log ‘This string is definitely empty’ when in fact the string is not empty, but because of the ! , it outputs the else statement. PLEASE HELP! I want to understand before I move on.


let favoritePhrase = ‘Just keep swimming’
if (!favoritePhrase) {
console.log(“This string doesn’t seem to be empty”)
} else {
console.log(“This string is definitely empty.”) // Output: This string is definitely empty.

/* Question: theres a variable after let that doesn’t contain one of the 6 falsey variables, so it makes it inherently a truthy variable, right? So why does a ! swap the output if the output is going to be incorrect? *\


Right. A non-empty string is truthy.

! (NOT) negates a truthy value to a boolean false.


The ! operator, depending on where you read this, is called the NOT operator. The NOT operator inverts a boolean value – so it will evaluate to TRUE if FALSE, and FALSE if TRUE.

Let’s look at your specific example:

let favoritePhrase = "Just keep swimming";
if (!favoritePhrase){  < - - - - - -  *** 
    . . . . .                                                 
    . . . . .

*** the if statement evaluates the expression inside the () and returns a TRUE or FALSE. When you are evaluating a variable that contains a string, it will return TRUE if the string is not empty. SO since you variable favoritePhrase is not empty, it returns TRUE. BUT the ! makes the expression FALSE, hence why the else statement are run.


ohhh okay, that’s what I understood was happening, but I thought I was wrong because it didn’t make sense to me as to WHY it would do that. So I guess my real question wold be. In what real life code example would their be a need to output in incorrect answer?

Why would someone use the ! to output the wrong answer? lol


Without getting into some code, the ! operator is often used when comparing two sets of things. With the ! operator you can determine if one thing is in one set but NOT the other set, among other things. The operator is also used often to make things logically easier to understand.

I encourage you not to understand this as ‘needing the incorrect answer’, but rather as ‘a way to manipulate the flow of the program’. After all, the program will do whatever you tell it to do.


Which cannot be stressed enough. We tell the computer what to do. We don’t guess what it will do. Expect an outcome, design code to produce that outcome.


Oh okay got it! I am new to this and have limited knowledge, I know there will be more to earn to help me fully understand as I go along, but thank you for helping me understand this basic thing so I can correctly process the next lessons! Thank youuuuuu!!! lol k bai.


We use logic to save coding, and streamline a process.

NOT is a different kind of logic than what comes naturally to most of us. Negating works both ways.

! true => false
! false => true

! 1 => false
+! 1 => 0

NOT will always return a boolean, regardless what kind of expression it is acting upon.

! 'string' => false
!!'string' => true


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.