Tips, Trick And Best Practices JavaScript
Tags: javascript, webdev, performance
Published: 2021-06-06 14:06:01

"Be persistent and never stop learning because life is a challenging race, you will receive countless valuable lessons in return." Hello Developers! Learning, accumulating new things every day helps us to improve our skills better and better. As a software developer, our job is to constantly learn new things, update technology, improve our own programming skills. In this article, I will learn some good tips, tricks, and methods in javascript.

###1. Multiple “if and else”,  Switch...case,  Object Literal.
With the code has many if else, else if, we will feel very stuck. So we will often think of switching to a switch case, which will look clearer and more beautiful. And for me, I also use the switch case.
![Example many if else](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9w7f36a7qcejgj105tt.png)
![Example Switch case and Object literal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78b8c7lenylea2sf68zt.png)
The switch case is good but it also has some problems like having to break to prevent it from continuing. And I find it still quite lengthy, with objects it is flexible, concise and easy to understand. Both methods have different advantages and disadvantages. I usually use switch case =). In some cases, use object.
###2. Destructuring, Spread Syntax and Rest Parameters.
These features help us work with data structures faster, more concise.
#####* Destructuring
Is a syntax that allows you to assign properties of an Object or an Array.
![example object destructuring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3a378aaaac3hgx5scta.png)
Above is an example of Object Destructuring, and below is an example of Array Destructuring.
![example array destructuring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1lrh0sfp8r2cv7jehmh.png)
#####* Spread Syntax
![example spread syntax](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfcmipfrsz7opoh9vmul.png)
#####* Rest Parameters
![example rest parameters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aplz9dpdes1hknqsqfs1.png)
###3. Optional Chaining and Nullish Coalescing 
#####* Optional Chaining
Check an attribute exists or not? If it does not exist, it will return undefined.
![example optional chaining](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xikdk3hb2uidbvwmp5h0.png)
Optional Chaining makes the code shorter, easier to access the properties of the object.
With array.
![example optional chaining with array](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khe53ec8ug6szpqs48xy.png)
#####* Nullish Coalescing
I often use the operator || to provide a default value for a variable. And now I still use it, hehe, but I have read the article carefully when using the or operator to provide a default value. It was also correct to read, then I went to MDN to read and know about Nullish Coalescing.
![example nullish coalescing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eyc26x4sf2cpp1hef6w.png)
Syntax: leftExpr ?? rightExpr
If leftExpr is nullish ( NULL or UNDEFINED ) the result will be rightExpr. If leftExpr has a value, the result is leftExpr.
![example nullish coalescing two](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td91w8zgctt815q1fcmm.png)
###4. Multiple Condition
You may have encountered a situation where there are many conditions that perform the same task. For example, with admin, leader or member rights, they all have the same rights (for example, the right to edit, delete posts, ...).
![Multiple Condition One](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eusl0hmgux5sguuxayt7.png)
We have many ways to make the code shorter, and look better.

![Multiple Condition Two](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxdc7sdcuk2xi3ddevq8.png)

I prefer to use includes :))
The article will still be updated with more cool things about javascript. Thank you for your support and reading until now. 
My blogs [hoangkhanh.tech](https://www.hoangkhanh.tech)
Thank you!