Writing clean JavaScript code: Variables
Stephanie Opala
Posted on February 5, 2021
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";
DO
let firstName = "Stephanie";
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();
DO
getUser();
- Use searchable names. This is helpful when you are looking for something or refactoring your code.
DON'T
setTimeout(blastOff, 86400000); //what is 86400000???
DO
const MILLISECONDS_IN_A_DAY = 60 * 60 * 24 * 1000; //86400000;
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
- Do not add unneeded context.
DON'T
const Laptop = {
laptopMake: "Dell",
laptopColor: "Grey",
laptopPrice: 2400
};
DO
const Laptop = {
make: "Dell",
color: "Grey",
price: 2400
};
Happy coding!
💖 💪 🙅 🚩
Stephanie Opala
Posted on February 5, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
webdev How to become a web developer in 2022. What I've learned from teaching myself web development.
February 1, 2022