-2

Is there a difference between !a and a===null in JavaScript? I am working on a project where it is behaving differently.

I am not talking in terms of Boolean values though as !true will not me same as true===null

if (!this.props.user) is resulting to true,

where as if (this.props.user === null) is resulting to false

Zohaib Hamdule
  • 540
  • 9
  • 23

2 Answers2

6

!a will return true if a is any Falsy value (e.g, false, undefined, null, 0...)

a === null will only return true if a is null.

Spectric
  • 30,714
  • 6
  • 20
  • 43
1

like @Spectric answer.
a === null need to have same type to return true.
even the value has falsy value (e.g, false, undefined, 0).
it will return false because the value is not same with null

krystallix
  • 121
  • 7