0

I currently have a variable that can be null as well as undefined. In both cases I want to leave the function:

if (myvar==null) return;

this works fine but I retrieve warnings from my IDE (VS) because == does not care about the type while === does. I am aware that == is generally be seen as bad style because of this.

Is this the exception from the rule? Should I simply ignore this warning and stick to the better readability or would the following be the recommend action?

if (myvar===null || myvar===undefined) return;
Ole Albers
  • 8,715
  • 10
  • 73
  • 166
  • If you're just checking if some value is truthy or falsy, `==` is fine, but in some situations you need to check if a value is specifically equal to `null` or `undefined`, in which case you must use `===`. This happens most often when working with third party libraries. But don't stress about these little things; just make it work! – TJBlackman Nov 21 '19 at 14:26
  • No. Ask your colleagues if they know the correct `==` semantics ... – Jonas Wilms Nov 21 '19 at 14:29
  • 2
    "*Should I simply ignore this warning*" no - [**change it**](https://eslint.org/docs/rules/eqeqeq#allow-null)! All linting rules are configurable, so I'm not sure why people are so reluctant to configure them to suit their needs. – VLAZ Nov 21 '19 at 14:31
  • `if (!myvar) return;` for falsy – Tunn Nov 21 '19 at 14:34
  • seems to me like you're just checking for "falsey" values. you can just use `if (!myvar) return;` – Kagiso Marvin Molekwa Nov 21 '19 at 14:35
  • @Tunn `myvar = 0` or `""` will be caught in those situations while `myvar == null` solves that potential bug. `myvar===null || myvar===undefined` is the same but it's honestly too long, especially if you've got multiple variables - `myvar1 == null || myvar2 == null || myvar3 == null` is much shorter. – VLAZ Nov 21 '19 at 14:39
  • @marvinIsSacul no, it's not falsy values -`myvar == null` is the same as `myvar===null || myvar===undefined` but not the same as `myvar===null || myvar===undefined || myvar===0 || myvar===""|| myvar===false` – VLAZ Nov 21 '19 at 14:40

2 Answers2

2

The problem is the lack of clarification. If you're writing the code for yourself, it's fine as you know exactly what you're doing. But if a contributor find this code, they could either:

  1. Not knowing how == works in case of null / undefined
  2. Knowing how it works but not knowing if the use was intentional (if you actually want to check both null or undefined, or you wanted just check for null but used ==)

The code should be clear, so often you find programmers adding comments before this check to specify that yes, they know what they're doing, and yes, it was intentional.

Eventually you get tired of that and start to use myvar === null || myvar === undefined.

It's more verbose, but it's clearer, and doesn't give room to misunderstanding.

Another thing I notice is creating custom fuction for such things, e.g. isNil(myvar) but despite the verbosity I prefer checking both values.

ZER0
  • 24,846
  • 5
  • 51
  • 54
1

== has very specific rules, and it normally recommended against because those rules are not intuitive. I would only condone using it if you have memorized the rules related to its behavior, as have all other devs who could possibly have to deal with your code (which most have not).

In this case, a == null returns true if a is one of null, undefined, or document.all, but not the other 4 falsely JS values (MDN, including why document.all is falsely). So unless you want it to match document.all, I would use ===.

Additionally, using === in all places adds consistency to your code.

Matthias
  • 648
  • 6
  • 18