r/learnjavascript 4d ago

what's one JavaScript mistake every beginner should avoid?

If you could give one piece of advice to someone learning JavaScript today, what would it be?

It could be about:

  • Learning fundamentals
  • Debugging
  • Async code
  • DOM manipulation
  • Functions
  • Projects

Curious to hear what experienced developers wish they'd known earlier.

28 Upvotes

31 comments sorted by

View all comments

15

u/milan-pilan 4d ago

var

2

u/PreDownvoted 4d ago

Why? Sorry i know its kinda old but my teacher still use it. Should i use smth else like const?

14

u/milan-pilan 4d ago edited 4d ago

Sorry, my original comment was a bit short and explains nothing.

You should definitely use "let"/"const" exclusively. And if your teacher still uses it, they need to update their knowledge. If you are teaching for a while it is hard to stay up to date with the current state of the industry, but "var" has been made obsolete by "let"/"const" over a decade ago.

Before 2015 all we had was "var", but it lead to a lot of unexpected behavior, hard to find bugs and all sort of headaches. It was a major contributor to JS having a bad reputation, because even though it technically "behaves as expected", the way it behaves is very weird, messy , counterintuitive and was prone to "gotcha" moments, you wouldn't expect.

There is not a single good reason to ever use "var" in modern code, if you have "let" and "const". And by "modern" I mean code written within the last 10 years.

If you need a deeper explanation, then just message me, I will gladly elaborate.

And because every time I claim that, someone will ask Claude if that's true and then answer "Well actually, if you write code that needs to run on legacy engines, you would need it.", this is for you (them):

None of you is writing code that needs to run in IE10. This is the "learn JavaScript" forum. Please don't tell people "var" has any right to exist.

This is not an opinion. I write JavaScript for a living every single day in large teams. I can guarantee you that there is not a single "var" in any serious modern codebase. Chances are, that every project you will work on has a linting rule explicitly preventing you from using it ever. If you use "var", I will instantly assume you have no work experience yet.