Confusion in !== Operator | JS | SoloLearn: Learn to code for FREE!

+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????

7/4/2020 9:51:32 AM

Himanshu Rai

12 Answers

New Answer

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

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

+6

Himanshu Rai I think Gordon meant the loose comparators are == and !=, not !==.

+4

!= is the loose one, !== is strict. He must have misspoke :)

+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 ~

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

+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

+2

Gordon no need to say sorry. I'm glad you have learned something new from others!

+2

Sharmin Rumpa Thanks for you explanation to make it more easy! 😊

+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

+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??????

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