20

Senior JS Dev where I'm contracting can't understand why null != "null"

Comments
  • 3
    Not equal to null?
  • 0
    Ya but there is no type checking in that statement. So it does not care that the right hand side is a string.
  • 0
    @programmingTrtl I think he was - for some reason - thinking that by using the double equals operator the string would somehow magically be coerced to null or that null would be coerced to a string value.
  • 1
  • 1
    @RTRMS Yes, there most definitely is type checking. The first thing that happens is both values are checked to see if they are the same type, if they are then they are evaluated against each other, if they're not the same type the abstract equality comparison algorithm is invoked.
  • 2
    "This, my friend, is far beyond my explanation skills"
  • 0
    @tnuhb well, an undefined variable is one which has not been defined in the current scope, a variable with the value of undefined means that the variable has been defined but has not yet been given a value, a variable with the value of null means that the variable has been defined but deliberately has no pointer to a value. Many databases/APIs return null as a value which would then get assigned to a variable.
  • 0
    I’m no js programmer but is that not checking a null ref against the string “null” that statement would always be true
  • 0
    @philcr in js there are === and !==, for strict equality comparisons. == and != check for truthiness and falsiness. For instance 7 == "7"
  • 0
    @devmg this is why I’ll stick to c#
  • 0
    @philcr are you suggesting this?
    null == "null" == true
  • 0
    Junior dev here. I guess it has to do something with stack and reference. Null is not assigned/ referenced to a value in stack and so second null.
    Since that those are not equal. Will check it tho.
  • 0
    @terminalterror I’m saying if you compare null to a string using != the condition would always be true because the string is not null
  • 0
    @philcr sorry, I misunderstood, and yes that's obviously going to be the case.
Add Comment