I was doing code review for a coworker yesterday, and it soon became obvious that he used a linter and that the linter gave him a bright idea: use strict comparisons.

Using strict comparisons is a great rule to follow. === instead of ==, !== instead of !=. Your life will be better.

You’re ensuring not only that your values are equal or unequal, you ensure their types match as well. Because JavaScript has funny types, strict comparison lets you avoid painful pain.

Things like this:

> "0" == 0

And like this:

> true == 1
> false == 0

Even things as silly as this:

> new Array() == 0
> [] == 0

Run a linter on those examples, and it will sagely say “Dude, use strict comparison. ALWAYS use strict comparison.”

And your linter would be right. === fixes all of those examples.

> "0" === 0
> true === 1
> false === 0
> new Array() === 0
> [] === 0

πŸ‘Œ Problem solved. πŸ‘Œ

But here’s one situation where it gets tricky. Checking undefinedness:

> null == undefined
> null === undefined

Under loose comparison, null and undefined are equal. Under strict, they’re not.

This can cause all sorts of issues.

Here are some examples I found in my coworker’s PR. πŸ‘‡

const url = $(elem).data('url')
if (url !== null) {
    // ...

But if your elem doesn’t have a data-url="..." attribute, jQuery returns undefined, not null. Strict comparison fails.

A better approach is to use if (url) because undefined is falsey and so is an empty string. That makes your code robust against data-url="" πŸ™‚

function scroll({ elem, offset, duration }) {
    duration = duration !== null ? duration : 2000;

But if you call scroll() without duration, it’s undefined, and your code breaks. No default duration for you. A better approach is to use destructuring defaults, like this: function scroll({ elem, offset, duration = 2000 }).

function get_id(widget) {
    let id = widget.id;
    if (id !== null) {
        // ...

But reading an inexistent object property returns undefined, not null, and this code breaks. Once more, you’re better of relying on inherent falsiness πŸ‘‰ if (id).

function createWidget(defaultText, onClick, markBusy) {
    new Widget({
        text: defaultText,
        onClick: onClick,
        markBusy: markBusy !== null && markBusy

This one is tricky. It’s trying to pass markBusy into the Widget constructor, but only if it’s defined. You can’t use default param values because there’s no destructuring so hmm… πŸ€”

Then again, the whole exercise is futile. You can achieve the same effect if you rely on inherent falseyness: markBusy: !!markBusy.

I guess my point is that you have to be careful. Don’t blindly trust your linter when it says change this code to that code.

Happy hacking πŸ€“

Learned something new? πŸ’Œ

Join 8,400+ people becoming better Frontend Engineers!

Here's the deal: leave your email and I'll send you an Interactive ES6 Cheatsheet πŸ“– right away.Β  After that you'll get an email once a week with my writings aboutΒ React, JavaScript,Β  andΒ life as an engineer.

You should follow me on twitter, here.