DEV Community

loading...

Writing clean JavaScript code: Variables

stephanieopala profile image stephanieopala ・1 min read

What is clean code? It is code that is easy to understand by humans and easy to change or extend.
In this post, I will cover JavaScript clean coding best practices when it comes to variables.

  • Use meaningful and pronounceable variables. You should name your variables such that they reveal the intention behind it. This makes it easier to read and understand.

DON'T

 let fName = "Stephanie";
Enter fullscreen mode Exit fullscreen mode

DO

 let firstName = "Stephanie";
Enter fullscreen mode Exit fullscreen mode
  • Use ES6 constants when variable values do not change.
    At this point, you have interacted with JavaScript ES6 severally/ a few times depending on your level of expertise therefore, keep this in mind.

  • Use the same vocabulary for the same type of variable.

DON'T

getUserInfo();
getClientData();
getCustomerRecord();
Enter fullscreen mode Exit fullscreen mode

DO

getUser();
Enter fullscreen mode Exit fullscreen mode
  • Use searchable names. This is helpful when you are looking for something or refactoring your code.

DON'T

setTimeout(blastOff, 86400000); //what is 86400000???
Enter fullscreen mode Exit fullscreen mode

DO

const MILLISECONDS_IN_A_DAY = 60 * 60 * 24 * 1000; //86400000;

setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
Enter fullscreen mode Exit fullscreen mode
  • Do not add unneeded context.

DON'T

const Laptop = {
 laptopMake: "Dell",
 laptopColor: "Grey",
 laptopPrice: 2400
};
Enter fullscreen mode Exit fullscreen mode

DO

const Laptop = {
 make: "Dell",
 color: "Grey",
 price: 2400
};
Enter fullscreen mode Exit fullscreen mode

Happy coding!

Discussion (0)

pic
Editor guide