True and False Values II


#1

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.

Example:

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? *\


#2

Right. A non-empty string is truthy.

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


#3

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){  < - - - - - -  *** 
    . . . . .                                                 
}                                                             
else{                                                                                      
    . . . . .
}

*** 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.


#4

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


#5

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.


#6

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.


#7

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.


#8

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

#9

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