+ 3

Confusion in !== Operator | JS

When I coded: Document.write(8 !== 8) It gives false. no confusion. Document.write(8 !=="8") It gives true. MEANS, !== Operator will give true if data type not same despite values are same. Document.write(8 !== 9) Now it also give true. There MEANS that !== Operator will give true if values are not same despite data type is same. So please tell me how this !== Operator works. It gives true if data type is dissimilar but value is similar, and it also gives true if values is not similar but data type is similar. What is the factor in this which dicides true. Is this a dissimilarity of data type or value????

4th Jul 2020, 9:51 AM
Himanshu Rai
Himanshu Rai - avatar
12 Respostas
+ 9
What does === do? 8 === "8" // false 8 === 8 // true 8 === 9 // false So, the types need to match and the value of course. !== just does the opposite, it gives false when === would give true and vice versa.
4th Jul 2020, 10:24 AM
Schindlabua
Schindlabua - avatar
+ 6
/* wrong explanation, just keeping to keep the conversation make sense */ Type coercion. "8" is converted from string to number when loose comparison == or !== operator is used Meanwhile, there is a strict comparison ===, where both type and value must match.
4th Jul 2020, 9:58 AM
Gordon
Gordon - avatar
+ 6
Himanshu Rai I think Gordon meant the loose comparators are == and !=, not !==.
4th Jul 2020, 10:33 AM
Russ
Russ - avatar
+ 4
!= is the loose one, !== is strict. He must have misspoke :)
4th Jul 2020, 10:34 AM
Schindlabua
Schindlabua - avatar
+ 4
sorry that i gave a wrong anwser making you more confused. actually I learned something new from Schindlabua's answer~ Thanks for both the question and the answer ~
4th Jul 2020, 2:08 PM
Gordon
Gordon - avatar
+ 3
Himanshu Rai I think or I remember it that way........ May be its help you === this operator check value & also data type. When value & data type are same it's true otherwise false. Now !== means not true. when (8 !== 8 ) that's said not true but actually ture coz 8 & 8 value same and also data type int. when (8 !== "8") that said not true and actually not true coz 8 & 8 same value but data type is not same, one is int and another is string. when (8 !== 9) that said not true and actually not true coz 8 & 9 not same value but data type same int.
5th Jul 2020, 6:28 AM
Sharmin Rumpa
Sharmin Rumpa - avatar
+ 2
Schindlabua Now that the answer! Gordon saying that !== Is loose operator and it converts "8" into 8. So according to Gordon, the result of below code should be false. Hence I'm confused. Code : 8 !== "8" Thanks for your clarification schindlabua
4th Jul 2020, 10:30 AM
Himanshu Rai
Himanshu Rai - avatar
+ 2
Gordon no need to say sorry. I'm glad you have learned something new from others!
4th Jul 2020, 2:26 PM
Himanshu Rai
Himanshu Rai - avatar
+ 2
Sharmin Rumpa Thanks for you explanation to make it more easy! 😊
5th Jul 2020, 6:44 AM
Himanshu Rai
Himanshu Rai - avatar
+ 2
== is the loose operator 69 == ‘69’ //true 69 == 69 //true != is the opposite of == 21 != ‘21’ //false 21 != 21 //false 21 != 69 //true === is the strict oprator, string can not be equal to numbers 420 === ‘420’ //false 420 === 420 //true !== is the opposite of === 420 !== ‘420’ //true 420 !== 420 //false 420 !== 69 //true
6th Jul 2020, 7:12 AM
Roberto Zunica
Roberto Zunica - avatar
+ 1
Gordon And thats my question. First read my code: 8 !== "8" According to you, "8" should be converted to 8. So result should be false. But when I run this code, the result is true. How??????
4th Jul 2020, 10:17 AM
Himanshu Rai
Himanshu Rai - avatar
+ 1
hello, there is == (or !=) operator performs an automatic type conversion if needed. The === (or !==) operator will not perform any conversion. It compares the value and the type, which could be considered faster than ==. That basically means that if 0 == "0" returns true, 0 === "0" will return false because you are comparing a number and a string. Similarly, while 0 != "0" returns false, 0 !== "0" returns true. I hope this will help you.
13th Jul 2020, 5:03 AM
Ishan Shah
Ishan Shah - avatar