Best Easy Practices of Javascript
Follow some best practices while writing your javascript code.

Who doesn’t like to follow best practices and write quality and maintainable code? Well if you don’t, that’s Ok. You will still find theses tips easy and quite helpful. And if you are enthusiastic about it, congratulations, you have landed in the right place!
Some may argue that users don’t care about code quality but more focused on results. But the code practices are created to save development time and reduce the number of bugs. So I have to disagree. Users *indirectly* care about how you write your code.
So without further ado, let’s begin.
Variables & References
- Use
let
instead ofvar
.
Why? Because var
is function scope which could lead to some bugs in your code while let
is block scope.
2. Use const
instead of let
when you don't have to reassign values.
According to Eslint
If a variable is never reassigned, using the
const
declaration is better.The const
declaration tells readers, "this variable is never reassigned," reducing cognitive load and improving maintainability.
3. Always use const
or let
to declare variables.
const
or letNot doing so will result in variables declared in the global scope and polluting global namespace.
4. Don’t chain variable assignments
Chaining variable assignments may seem easier to code but have repercussions associated with it. During chaining of variable assignments, implicit global variables are created which results in polluting the global namespace.
Arrays & Objects
- Creating new array & objects without
new
While there are no performance differences between the above two approaches, the conciseness of the object literal form is what has made it the way of creating new objects
2. Use object property/method shorthand
ES6 provides a cleaner form of defining object literal methods and properties. Also, group your shorthand properties at the beginning of your object declaration. This syntax can make defining complex object literals much easier and more maintainable.
3. Using spread
operator to shallow copy array and objects.
Shallow copy array using the spread operator
Destructuring
- Use object destructuring when accessing and using multiple properties of an object.
Destructuring prevents you from creating temporary references for those properties of objects.
2. Use array destructuring
Functions
- Never declare a function in a non-function block (
if
,while
, etc).
Instead of defining a function inside a non-functional block, declare a variable and assign the function to it based on the conditions if present.
2. Prefer using the ...
syntax instead of using arguments
Why? Because rest arguments are a real Array, and not merely Array-like like arguments
.
3. Use default parameters rather than mutating the function arguments.
Conclusion
That’s all for now folks! I hope the above simple practices will help you in writing clean and maintainable code throughout the project. Happy clean coding.