Why does the program show 'true' for both conditions?


#1


https://www.codecademy.com/en/courses/getting-started-v2/3/1?curriculum_id=506324b3a7dffd00020bf661

From what I know, if the first condition is not fulfilled, the program will show 'false' when the second condition if fulfilled. However, when I run the code below, it shows 'true' for the second condition as well.

Also, can I use "==" instead of "===" to show that the values are equal? I have some experience from coding Javascript and "==" was taught to me. Is there any difference?

if (12 / 4 === "Ardi".length) {
    confirm("Will this run the first block?");
} else {
    confirm("WIll it run the second block?");
}

#2

i think it shows true because confirm is considered true, which you can see like this:

if (confirm("this works")){
    console.log("confirm is considered true")
}

yea, there is a difference between two or three equal signs. two doesn't compare datatype, so:

"true" == true
"3" == 3

are both true, while:

"true" === true
"3" === 3

those are both false (string doesn't match boolean, and string doesn't match integer), so there is a difference


#3

Could you give me examples on when will each sign(==/===) be applicable in a 'if' function?


#4

it is the programmers choice when to use == or ===, the relevant question is if you want to compare data types. You can use either one of them inside a if condition, the choice is yours


#5

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