Skip to main content

Command Palette

Search for a command to run...

These things you should keep in mind while writing JavaScript

Published
β€’3 min read
These things you should keep in mind while writing JavaScript
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

Hello Folks πŸ‘‹

What's up friends, this is SnowBit here. I am a young passionate and self-taught frontend web developer and have an intention to become a successful developer.

Today, I am here with a few important things that you should keep in your mind while writing your JavaScript code.

Happy Reading!


Writing clean code is not a code that works, it's about readability, reused and refactored by other developers. In reality, you are not writing for yourself, you are writing for other developers who can easily read your code who can understand your code easily without any trouble editing or contributing to the project.

In this article, I will be focusing on why and how you should write clean code.

Camel Case

Camel case is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, and the first word starting with either case.

Source: Wikipedia

// Don't do this ❌
const isadmin = true

// Do this βœ…
const isAdmin = true

Meaningful variable names

While giving names to the variable, you should not give irrelevant or meaningless names; You should give proper names to the variable according to its job.

// Don't do this ❌
const foo = ["cars", "boats", "planes"]

// Do this βœ…
const vehicles = ["cars", "boats", "planes"]

Boolean

When assigning a boolean to a variable, the name of the variable must be in interrogative tone.

The reason why you should name a boolean variable in an interrogative tone is that it is easy to find the type of the variable in little code but if you want to check the type of that variable then it becomes pretty time consuming to find that variable type and assign it a new value.

// Don't do this ❌
let sunday = true

// Do this βœ…
let isSunday = true

Here, you can clearly determine the type of variable - isSunday as you are asking Is it Sunday? and the answer would be yes (true) or no (false) but in the case for variable sunday you can't determine the type of that variable without viewing, here sunday can be anything - I love sunday or I play cricket on sunday.

const isAdmin = true
const hasSubscribed = true
const hasTwitchLinked = false

Avoid extra and unnecessary context

Don't add unwanted information to the variable name when the context is provided by the object or a class

// Don't do this ❌
const user = {
   userName: "SnowBit",
   userAge: 15,
   isUserAdmin: true
}

// Do this βœ…
const user = {
   name: "SnowBit",
   age: 15,
   isAdmin: true
}

Avoid hardcoded values

It's better to use meaningful variables to store constant values instead of hardcoded values. It is better to keep global constants in Upper Snake Case - UPPER_SNAKE_CASE

// Don't do this ❌
const areaOfCircle = 3.14 * (4)^2

// Do this βœ…
const RADIUS_OF_CIRCLE = 4
const areaOfCircle = 3.14 * (RADIUS_OF_CIRCLE)^2

So, these were some important tips that I mind sharing with you folks and I hope you enjoyed reading the article. I will be making second part of this article soon so don't forget to follow me.

Thank you for reading, have a nice day! Your appreciation is my motivation 😊

More from this blog

D

Dhairya Shah

119 posts

These things you should keep in mind while writing JavaScript