DEV Community

Stephanie Opala
Stephanie Opala

Posted on

14 1

Writing clean JavaScript code: Variables

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!

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
muriukialex profile image
Alex

Descriptive variable names are really important for maintenability of code. 👏

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay