JavaScript is powerful and flexible, it allows you to write code any way you like and try some very unusual things that can lead to bugs in your code. Here are 50 things I learned about coding in JavaScript that you should know.
Always “use strict” on
If you use any library/framework or compiler for JavaScript, “use strict” may be enabled, but remember to add it to your files and functions just in case. If you don’t include it, it will ensure you get errors that will happen silently.
Use function expressions instead of function declarations
Function expressions are preferred unless you want to take advantage of function behavior and properties. Function declarations are hoisted, and while it’s sometimes useful, avoid them because they can give code strange behavior and it’s not always obvious what’s going on. Try to figure out where the functions you’re using are coming from and where they’re coming from before you use them to avoid weird accesses.
Stop using “var”!
Declarations with “var” are also hoisted, which makes the var declaration accessible before the declaration occurs, which is a strange, non-obvious behavior.
Use “const” and immutability wherever possible
Choose immutability whenever possible. Constantly changing and passing data around can make tracking bugs and the changes themselves difficult. Handle data copies and avoid side effects.
prefer pure functions
Moving on to side effects, make sure your functions don’t change the data that calls them or the data in the scope that created them.
Prefer classes over constructors
While a constructor allows you to do some really nice things, if you find yourself reaching for its prototype, it’s a sign that you need to use a “class” that is supported almost everywhere. It’s cleaner and easier for people to understand.
Use “destructuring”
Destructuring is elegant, it makes your need for arrays and objects more obvious, and it also gives you the opportunity to rename things to help make your code more meaningful.
Use only the data you need
Like the example above, destructuring is a great way to extract the data you need to get the job done, but also get in the habit of calling methods and functions with only what they need. This also applies to data from the API. Extract and clean only the data you need before storing or doing anything with it.
Always use “===”
Triple equality checks for value and type, which is what you’ve always wanted to do. Get in the habit of always triple-checking and avoiding bad effects.
avoid global variables
Avoid creating things in the global object unless you are creating a library/framework. Global property names can collide with things also introduced by third parties or colleagues, and are difficult to debug.
Wrap loose declarations in blocks
You can avoid name collisions and loose temporary declaration access by wrapping the fast logic in its own scope.
Organize your statement
Be consistent with how you declare things. Put all declarations first, starting with constants and continuing to variables. All uppercase constants to indicate they are constants will prevent developers from trying to change them.
Don’t initialize things with “undefined”
…
The post 50 JavaScript Best Practice Rules for Writing Better Code first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/9839
This site is for inclusion only, and the copyright belongs to the original author.