0

I have no idea about this crazy code.

const x = [0];
x == x // True. I understand this
x == !x // True ??? 

I cannot understand what happen inside javascript and I need someone explain me this case.

Hoang Subin
  • 6,610
  • 6
  • 37
  • 56
  • 3
    This is under-researched. Why is it upvoted? If you did research, you should include all of the stackoverflow pages that were related but that failed to explain things sufficiently for you. – mickmackusa Jan 17 '19 at 04:27
  • 3
    Type coercion bro, [0]==![0] gets converted to 0 == false, and then false == false, which is true. – Danyal Imran Jan 17 '19 at 04:29
  • Yes. I understand about == and === in javascript. But My question is about == operator only. What actually happen with this example? Since this creates bugs in many cases @mickmackusa – Hoang Subin Jan 17 '19 at 04:29
  • It only creates bugs when a programmer is using `==`. Easy fix: never, ever, use `==`, there's no reason to. – CertainPerformance Jan 17 '19 at 04:30
  • https://medium.freecodecamp.org/js-type-coercion-explained-27ba3d9a2839 and https://stackoverflow.com/q/19915688/2943403 and https://stackoverflow.com/q/4626361/2943403 – mickmackusa Jan 17 '19 at 04:33
  • @DanyalImran I think your answer is right. I tried to create another variable `a = [2]` and `a == !a` return FALSE. – Hoang Subin Jan 17 '19 at 04:33
  • @mickmackusa Thank you so much. I got the key work `type coercion` for this. – Hoang Subin Jan 17 '19 at 04:35

0 Answers0