Some useful JavaScript tips

Tram Ho

Like every other programming language, JavaScript has dozens of tricks to perform both easy and difficult tasks. Some tips are widely known while others are enough to blow your mind. Let’s take a look at some JavaScript tips that you can start using today!

1. Clearing or truncating an array

An easy way to delete or truncate an array without reassigning it is by changing the value of its length attribute:

2. Object destructuring for array items

Assign array items to individual variables with object destructuring

3. Creating pure objects

You can create a 100% pure object, it does not inherit any properties or methods from the Object

4. Get Unique Values ​​of an Array

Taking out an array with elements is unique, I have 1 article on Set vs Array

5. Array and Boolean

Do you ever need to filter falsy values ​​(0, undefined, null, false, etc.) from an array? You may not know this trick:

6. Converting to boolean using !! operator

Sometimes we need to check if some variables exist or if they have a valid value, to see if they are true. !! will help us to test this with an extremely concise syntax. For example

7. Converting to number using + operator

This + operator also works with Date

Share the news now

Source : Viblo