"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.
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.
Above is an example of Object Destructuring, and below is an example of Array Destructuring.
* Spread Syntax
* Rest Parameters
3. Optional Chaining and Nullish Coalescing
* Optional Chaining
Check an attribute exists or not? If it does not exist, it will return undefined.
Optional Chaining makes the code shorter, easier to access the properties of the object.
With array.
* 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.
Syntax: leftExpr ?? rightExpr
If leftExpr is nullish ( NULL or UNDEFINED ) the result will be rightExpr. If leftExpr has a value, the result is leftExpr.
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, ...).
We have many ways to make the code shorter, and look better.
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
Thank you!
Top comments (0)