---
title: "Discussion on \"Javascript Clean Code Tips & Good Practices\" | Hashnode"
description: "Discussion on \"Javascript Clean Code Tips & Good Practices\". 15 Tips I Follow For Writing Better Code"
url: "https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Javascript Clean Code Tips & Good Practices](https://apoorvtyagi.tech/javascript-clean-code-tips-and-good-practices?utm_source=hashnode&utm_medium=feed)

15 Tips I Follow For Writing Better Code

Aug 22, 2021 · 8 min read · apoorvtyagi.tech  
Updated Aug 23, 2021  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#clean-code](https://hashnode.com/tag/clean-code)
-   [#best-practices](https://hashnode.com/tag/best-practices)
-   [#programming](https://hashnode.com/tag/programming)
-   [#nodejs](https://hashnode.com/tag/nodejs)

## Written by

[Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

@apoorvtyagi · India

  
[View profile](https://hashnode.com/@apoorvtyagi)

Senior Software Engineer | Tech Blogger | !(Chess GM)

Senior Software Engineer. I write about practical tips and epic stories about my experiences in the field of computer science and engineering!

## Responses 6

Markdown supported · ⌘↵ to post

[Shubham Kumar Singh](https://hashnode.com/@shubhamsinghbundela)

[Aug 27, 2022](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/630a892438843b713978700b)

Thank you very helpful [Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

[Roel Magdaleno](https://hashnode.com/@roelmagdaleno)

[Aug 23, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/6123e573b115357df05f2708)

Awesome article. Clean code and good practices should be a must topic to learn.

One opinion, though. I think **1\. Conditionals** needs more explanation about why is wrong to use a negative condition because **negative conditions are frequently used in Guard Clauses**.

Actually, Tip #1 and #14 would be similar.

Thanks!

[Alifer de Medeiros Quevedo](https://hashnode.com/@goufix)

[Aug 23, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/6123acefb115357df05f1ef1)

I really liked this article.

I just got a point that in the section 08 the good example have a simple error. That code:

```
const { add, subtract } = require('./calculations')

add(4,2);
calculate.subtract(4,2);
```

should be:

```
const { add, subtract } = require('./calculations')

add(4,2);
subtract(4,2);
```

[Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

[Aug 23, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/6123acefb115357df05f1ef1#reply-6123aec7663d427e19ff83bb)

Oh yeah, It was a mistake on my end. Thanks for letting me know :) 👍

[Bento](https://hashnode.com/@bento)

[Aug 23, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/61238b38b115357df05f1984)

Thank you for the article.

It seems like point 14 example doesn't follow the rules you talk about yourself. For example:

```
function isPercentage(val) {
 ...
}
```

This function should be named `checkIfInRange` or something that makes it more obvious that it is a function. To me as a developer something that is prefixed with `is`, will most likely be a `boolean`. In my opinion a better approach at this is:

```
function checkIfInRange(amount) {
  return (amount >= 0 && amount <= 100);
}

const isInRange = checkIfInRange(78);
console.log(isInRange); // true
```

[Bento](https://hashnode.com/@bento)

[Aug 23, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/61238b38b115357df05f1984#reply-6123c44d663d427e19ff86d3)

[Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

Indeed `isPercentage()` is but `isPercentage` isn't. In my opinion functions should be named to the actions they are taking and not what their return values will be, for example `getUser, checkNaming`, otherwise if you name the function isPercentage what will you name the return value?

```
function isPercentage(val) {
  var isInRange = (val >= 0 && val <= 100);
  return isInRange;
} 

const ??? = isPercentage(9);
```

So the naming for the function above is bad 👎 as it implies a boolean type but is a function.

Also, that function does not check if a value is a percentage, I'd expect that if a function is called isPercentage it would test if a string contains a value like `%` or a logic around it.

Naming a function like this will lead to confusion and the point of the article is to leave things clean and with good practices, you advocate good practices and explain them in other points but this is not one of them.

There's no such thing as _boolean functions_, it's just a `function` that returns a `boolean`.

`typeof(isPercentage)` is `function` and not `boolean`.

[Dylan Watson](https://hashnode.com/@dylanwatsonsoftware)

[Aug 25, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/61238b38b115357df05f1984#reply-6126ce7971cb04254f06c1ea)

[Bento](https://hashnode.com/@bento) it actually took me a little while to get what you were saying... But it makes some sense. I've never thought about it like that before. I definitely have had that issue where I end up wanting to name the variable the same as the function.

Also... Great article [Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)!

[Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

[Aug 26, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/61238b38b115357df05f1984#reply-6127b45571cb04254f06d9e0)

Thanks [Dylan Watson](https://hashnode.com/@dylanwatsonsoftware)

[Anish De](https://hashnode.com/@AnishDe12020)

[Aug 22, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/6122800be404793819cab2de)

Now that I have set the thumbnail of this post as my wallpaper, I think I won't make the mistake again

[Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

[Aug 22, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/6122800be404793819cab2de#reply-6122814fe404793819cab313)

That's a good way to remind yourself 😁👍

[Eslam Ahmed](https://hashnode.com/@Eslam-Ahmed)

[Aug 22, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/61226553a5d08237f40e2346)

I really found this helpful, Thank you

[Apoorv Tyagi](https://hashnode.com/@apoorvtyagi)

[Aug 22, 2021](https://hashnode.com/posts/javascript-clean-code-tips-and-good-practices/612221e8a5d08237f40e1bca/comment/61226553a5d08237f40e2346#reply-612265d4e404793819cab06f)

Glad to hear that. Thanks a lot 🙌
