JavaScript Tips, Tricks, and Best Practices
Tags: javascript30, javascriptdevelopmen, javascriptframeworks, devopspractice
Published: 2022-11-26 13:44:24

---
title: JavaScript Tips, Tricks, and Best Practices
published: true
date: 2022-11-25 15:37:16 UTC
tags: javascript30,javascriptdevelopmen,javascriptframeworks,devopspractice
canonical_url: https://devangtomar.medium.com/javascript-tips-tricks-and-best-practices-22a9181e2b2f
---

![](https://cdn-images-1.medium.com/max/554/0*1vgTIv2moSO2jNJm.jpg)

JavaScript, along with HTML and CSS, is one of the three essential components of the internet. Any website you visit will almost certainly utilize a combination of these three programming languages, each serving a specific function.

JavaScript can be found anywhere.

![](https://cdn-images-1.medium.com/max/833/0*x8ccGs_5hd7xSl_P)

If you care about the code itself and how it’s written, rather than just whether it works or not, you may say that you practice and value clean code.

> A professional developer will write code for both the future self and the “other guy,” not just the machine.

Based on this, clean code can be defined as code that is self-explanatory, easy to understand by humans, and easy to change or extend.

![](https://cdn-images-1.medium.com/max/666/0*dmsbMqLNQClLOjp5)

The focus of this post will be on **JavaScript** , _but the techniques can be extended to other programming languages as well._

#### 1. When using the loose equality operator, be cautious.

If necessary, the Loose Equality Operator (== OR !=) conducts automated type conversion before comparison.

Use === instead of ==

Loose Equality Operator can produce surprising results, as shown in the above example.

![](https://cdn-images-1.medium.com/max/676/1*47Iinb4fbIDJOnyl7WwFxQ.png)

#### 2. Simple method for swaping two variables

Because it is concise and expressive, employ the destructuring assignment strategy. Swapping is accomplished using a single line of code. It supports any data type, including numbers, texts, booleans, and objects.

![](https://cdn-images-1.medium.com/max/652/1*kbMPJxpmt43XlYaEsFvbcA.png)

#### 3. Replace if true statements with &&

&& operators are less commonly used, but will become more common in the future.

**Bad** 👎

![](https://cdn-images-1.medium.com/max/652/1*aKwUaVwgjTQHMJ9dKKcqFg.png)

**Good 👍**

![](https://cdn-images-1.medium.com/max/720/1*mPpAPBPTcpPrt08xKsrEtw.png)

#### 4. Passing arguments as objects

![](https://cdn-images-1.medium.com/max/772/1*kZY8_moRww_MLxbF7nEmhw.png)

This method of presenting arguments has numerous advantages :

- The order of the parameters is no longer important, allowing you to focus on delivering high-quality code rather than repeatedly checking the function definition.
- The IDE will focus on the specific argument that you are providing, making auto-completion easier.
- As function calls specify the value of each property, this method clearly communicates intent.
- Large codebases will benefit greatly from the increased verbosity.

#### 5. Format JSON output with spaces

![](https://cdn-images-1.medium.com/max/772/1*RUkNWPN3cFeqQL2MG9SzTA.png)

A simple yet powerful tool for exporting readable JSON by specifying the number of spaces to use for indentation in the third parameter.

The second parameter is the replacer, which can be either a function that controls the stringifying process or an array that specifies the names of the properties that should be included in the stringified output.

#### 6. Use the spread operator to shallow copy objects (and arrays!)

![](https://cdn-images-1.medium.com/max/1024/1*p1aaSxEf8JYucPbtomdxmg.png)

The spread syntax in JavaScript has made it easier than ever to expand objects or arrays and conduct copies.

It is extremely useful for performing state management in React or React Native because all you have to do is copy the current state using the object literal, adjust your chosen properties, then update the state with the useState state hook.

It’s also a great technique to concatenate arrays or merge objects with a single line of code, rather than having to go over each instance and merge manually.

#### 7. Remove duplicates from arrays using Set

![](https://cdn-images-1.medium.com/max/1024/1*RHUXneKSNR0_sPdqbeapNg.png)

A basic but very effective one-liner approach for deleting duplicates from arrays.

In this example, we also used the newly demonstrated spread operator to expand the set and produce an array.

This method works with values of any type and even handles some of JavaScript’s strange equality quirks.

Sets can also be used to remove duplicates from complex object arrays.

#### 8. Use reduce() map() and filter() instead of regular for loops

**Use the reduce() method to reduce an array to a single value.**

![](https://cdn-images-1.medium.com/max/968/1*PPyMZUfNb2SpX2kYjfZVbQ.png)

**Use the map() method to create a new array with the results of calling a function for every array element.**

![](https://cdn-images-1.medium.com/max/968/1*CsfGg0X7ZEA6V7krOaXi9g.png)

**Use the filter() method to create an array filled with all array elements that pass a test (provided as a function).**

![](https://cdn-images-1.medium.com/max/968/1*J8ZlOJPQGrhap6s8nvmzJA.png)

#### 9. Conditional Operator

Any **if..else** statement can be changed to a conditional statement using the following syntax :

```
condition ? (expression if true) : (expression if false)
```

For example, the following code :

![](https://cdn-images-1.medium.com/max/968/1*txiNgHxyDYqjUoowNpw7BA.png)

Can be reduced to :

![](https://cdn-images-1.medium.com/max/1024/1*BVa4ujAEd3kl5CfcoK8UMg.png)

#### 10. Strings on Steroids

Embed a variable in-between a string :

![](https://cdn-images-1.medium.com/max/886/1*m7zRcpv7U0HVj3SxpTTcIQ.png)

#### 11. Convert a Number to a String

![](https://cdn-images-1.medium.com/max/764/1*nGaxIh6U-3zIA0uuldTDhA.png)

#### 12. Convert a String to a Number

![](https://cdn-images-1.medium.com/max/764/1*OLa71N_6rWzWmz9yunSPyw.png)

#### 13. Split a String into an Array

![](https://cdn-images-1.medium.com/max/840/1*pAtln9uHkGsyPD48WJshWg.png)

#### 14. String to a number using the plus (+) operator

![](https://cdn-images-1.medium.com/max/1024/1*pzN_HOaQ7iuhweoTD6szNw.png)

The **_slice()_** method returns selected elements in an array, as a new array. Negative numbers select from the end of the array.

The **_padStart()_** method pads the current string with another string until the resulting string reaches the given length. The padding is applied from the start of the current string.

Masking is possible with less code.

I have also compiled this as a readme over my GitHub. Fancy checking it out? here it is.

### GitHub URL for this article 💻

[GitHub - devangtomar/js-tips-and-tricks](https://github.com/devangtomar/js-tips-and-tricks)

### Conclusion 💡

Thank you for reading; these are some of the best JavaScript developer tips and best practises to follow for increased productivity and code readability.

![](https://cdn-images-1.medium.com/max/500/0*_7wIRqqopAlNbY79.jpg)

### Let’s connect and chat! Open to anything under the sun 🏖️🍹

**🐦 Twitter :** [devangtomar7](https://twitter.com/devangtomar7)  
**🔗 LinkedIn :** [devangtomar](https://www.linkedin.com/in/devangtomar)  
**📚 Stackoverflow :** [devangtomar](https://stackoverflow.com/users/8198097/devangtomar)  
**🖼️ Instagram :** [be\_ayushmann](https://instagram.com/be_ayushmann)  
Ⓜ️ **Medium :** [Devang Tomar](https://medium.com/u/8f5e1c86129d?source=post_page-----e42119a306ca--------------------------------)  
☊ **Hashnode :** [devangtomar](https://devangtomar.hashnode.dev/)  
**🧑‍💻 Dev.to :** [devangtomar](https://dev.to/devangtomar)