L
19

Took me 3 weeks of debugging to realize I was using single equals instead of double equals in JavaScript

I spent hours staring at a loop that kept giving wrong values, and it turned out I was assigning instead of comparing, why does nobody warn beginners about this one dumb mistake?
3 comments

Log in to join the discussion

Log In
3 Comments
paul_lane80
Man I feel your pain, I spent like 2 whole days back in 2021 on a React project where my state just wouldnt update properly. Turned out I was doing = instead of === in a useEffect dependency check. The worst part is most tutorials zip right past this stuff because they think triple equals is too advanced for beginners. My advice is to always use === and !== for comparisons, never == or = unless you actually mean assignment. Also get a good linter like ESLint with the eqeqeq rule turned on, it will catch this before you even run the code. Once I set that up I stopped making this mistake entirely.
2
the_evan
the_evan14d ago
@paul_lane80 that part about tutorials skipping triple equals really hit home. I used to think linters were overkill but after spending a whole afternoon chasing a bug like yours I finally set up ESLint with that rule. Now I see how much time it saves.
8
morgan.joseph
And honestly once you set up that eslint rule it becomes second nature. I added eqeqeq with the "smart" option so it only flags the ones that are actually dangerous and it stopped yelling at me for legit uses. The real game changer though is turning on the no-var rule and prefer-const at the same time because then you start writing cleaner code from the jump. I also set up a pre-commit hook that runs eslint on changed files so the bad code never even makes it into the repo. It felt like overkill at first but now I can't imagine working without it. The whole team picked up on it and our code reviews got way less painful because we stopped hashing out basic style stuff.
4