iArmend.de - Blog
Always “use strict” On
Use Function expressions instead of Function Declarations
Unless you want to take advantage of Function behavior and properties, prefer function expressions. Function declarations are hoisted and although it can be useful sometimes, avoid them as they introduce weird behavior to the code and it is not always obvious what’s happening. Try to make it clear where the function you are using comes from and they come before you use them to avoid weird access.
Stop using “var”!
Declarations with “var” are also hoisted which makes var declarations be accessible before where the declaration happened which is weird, non-obvious behavior.
Use “const” and immutability as much as possible
Prefer immutability as much as possible. Constantly changing data and passing it around can make it hard to track bugs and the changes itself. Work on data copies and avoid side effects.
Prefer Pure Functions
Continuing on the side effect note, ensure your functions are not changing data they are called with or data in the scope where they are created.
Prefer Class over Constructor Functions
Although the constructor function allows you to do some very nice stuff, if you find yourself reaching out for its prototype is a sign you need to use “class” which are supported pretty much anywhere. It is cleaner and something people are more likely to understand.
Destructuring is elegant and makes it more obvious what you need from array and objects and also gives you the opportunity to rename things to help give more sense to your code.
Only work with data you need
Like the above examples, destructuring is a good way to extract the data you need to do the job but, also make it a habit to only call methods and functions with the things they need. This also goes to the data coming from the API. Extract and cleanup only the data you need before storing or doing anything to it.
Always use “===”
The triple equal checks for value and type and it is something you always want to do. Make it a habit to always triple check and avoid undesirable effects.
Avoid Global Variables
Avoiding creating things in global objects unless you are creating a library/framework. Global property names may collide with third parties or something a colleague also introduced and are hard to debug.
Wrap loose declarations in blocks
You can avoid name clash and loose temporary declarations access by wrapping a quickly logic in its own scope.