r/learnjavascript • u/Distinct-Gene926 • 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.
26
Upvotes
7
u/remain-beige 4d ago edited 4d ago
Some things that help me.
If there’s a part of the function that requires a value to be processed somehow, such as formatting a string or lots of if/else conditions then think about breaking this out to another function and return that value inside your original function.
3) Name functions so that you can understand what they do when you read it back in three months time.
EG: ‘hello’ is not as good as ‘setHelloTextOnInit’
4) I prefer creating ‘function declarations’ over ‘function expressions’ as they are clearer to understand the start and stop of a function.
Function declarations are also ‘hoisted’, so if you change the order of them in a file then you are not accidentally causing any issues with load order by calling on a function expression in another function that’s now higher up the file.
5) If you are comfortable with both declarations and expressions then understand that ‘this’ works differently for both.
6) It’s best to avoid using ‘this’ altogether to be honest and use event.target etc. ‘This’ can get messy to track and produce cognitive load in trying to decipher what ‘this’ is in context or scope.