Basic JavaScript - Comparison with the Inequality Operator - Examples!

Tell us what’s happening:

Can someone Explain me the last two examples of this challenge?
I’ve completed this challenge! But confused with the example in the instructions.

How does 1 != true return false.
If 1 is not equal to true, it should return true right???
And similarly in 0 != false, it should return true right???

Or Am I missing something?

Your code so far

 This is not the code of the problem. This is the example

1 !=  2    // true
1 != "1"   // false
1 != '1'   // false
1 != true  // false
0 != false // false

Challenge Information:

Basic JavaScript - Comparison with the Inequality Operator

Your intuition is not really wrong, because how 1 could be equal to true? The thing is, when not using strict comparison (===) JavaScript will attempt to figure out, whether the values from different types might be comparable after type conversion. This is basically the same reason why 1 == '1' will be true, however 1 === '1' is false.

Take a look:

console.log(1 == true);   // true
console.log(1 === true);  // false
console.log(1 != true);   // false
console.log(1 !== true);  // true

console.log(0 == false);  // true
console.log(0 === false); // false
console.log(0 != false);  // false
console.log(0 !== false); // true
2 Likes

But 1 is a number and true is text right?

true is a Boolean, notice there’s no quotes or double quotes around it.

1 Like

Ohhhh…
So Boolean’s can be used directly in comparisons?
Then what value would JavaScript assign to the boolean true.?

Edit: I kind of understand now…
does boolean true have a value of 1?

Edit 2 : With some web search i understand it now completely?

Thanks @sanity :heart: :heart: :heart:

2 Likes