Program in Javascript like a Pro
Tags: webdev, javascript, cleancode, programming
Published: 2024-01-08 17:22:48

[Versión en español](https://dev.to/miasalazar/programa-en-javascript-como-una-persona-pro-42c5)

The other day I was thinking about how I could learn to code better, and write it in a way that is more reusable and clean. I was watching many videos and reading various articles and in the end I thought, what if I put together everything I have been learning in one article?... And here we are:

- Avoid global variables: Global variables are a bad idea. There's a good chance you'll end up overwriting them by accident; It is more difficult for Javascript to find them compared to the local ones, in addition to [other reasons that you can see in this thread](https://stackoverflow.com/questions/10525582/w).
- Comment: The code should be self-explanatory... But adding some comments doesn't hurt anyone.
![A comment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysgnv0r632bv8r0ukjg9.PNG)
- Avoid accessing the DOM: It is very expensive, so avoid it as much as possible.
- Don't trust: Validators are good friends and avoid errors when for some reason something fails.
- Libraries are our friends: Used well, they help us develop faster and with less code.
- Language and framework difference: Libraries and frameworks are very useful, but it is necessary to be able to distinguish one from the others and know what can be done with each one.
- Use tools that unify your code style like [eslint](https://eslint.org/).

![eslint warning example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vs7yhvt3kwl7d71oztn.PNG)
- Modularize: Create functions that do one thing.
- Avoid nesting too much: Generating complex code makes it very difficult to maintain and understand it.
- Take care of your loops: Try to make the fewer and more optimized loops, the better. Instead of a for or while you can use find, reducer, map or filter when convenient. They are better.
- Stay up to date: The new Javascript specifications have functionalities like [spread operator](ttps://dev.to/alextomas80/el-operador-spread-en-java) or the [rest operator](https://developer .mozilla.org/es/docs/Web/Ja) that make programming easier.
- Use let and const: Both are more optimal and avoid overwriting and scope errors.
- Test: Helps on many levels.
- Be curious: Many times we don't wonder how something works until it stops working. It's good to research the code and understand it even when it does work.

![SpongeBob meme with "it works on my machine"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/icxxr5234uqym57phvdt.jpg)

- There is a world beyond the console.log: Without a doubt, the console.log is everyone's favorite, however, there are many others that we can use and that will be more useful.
- Name your variables well: Your future self will thank you when they have to understand what you did. And so do your colleagues.
- Be consistent in the naming: If you decide camelCase, kebab-case, or any other... continue with it. Standardizing names helps you find things better and makes your code cleaner.
- Avoid duplication: If you have duplicate code, you are doing something wrong.

![Three spidermans pointing each other](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngl2l434owuyginpvb5f.PNG)
- Use strict mode: Javascript is a language that allows you many things without complaining. This is useful for getting something to work, but it can lead to strange behavior.
![_use strict](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zppbxwx44ux3ixrznig8.PNG)
- Specify default values: This will save you from many careless errors, and will allow you to indicate that a property or parameter is optional.
- Use template literals: They are much more comfortable when handling strings with variables.