7 JavaScript Tricks You Should Know

These tips became treasures when I found out about them

Adarsh gupta
4 min readJan 23, 2022
kraken images on unsplash.com

1. Sum all the values from an array

Suppose we have an array of numbers: let numbers = [2,52,55,5].

To get the sum we usually use a for loop and traverse through the list right

You can easily do that with this line of code: let sum = numbers.reduce((x,y) => return x+y).

And you can print the result via console.log(sum).

2. Reduce the length of an array using an array length property

We can reduce the size of the array using the array.length() method,

Let's see the code for that:

let array = [11,12,12,122,1222]

We now have an array of 5 elements. array.length will return 5.

Now suppose I want to reduce the length of an array we can just do it by using array.length = 4 now when you print the array your array will consist of [11,12,12,122] only.

3. Shuffle array elements

We all need to get random data sometimes. But sometimes we need to get random data from a specific dataset.

At that time we can use the below snippet that will save your time:

4. Filter unique values

Sometimes we need to filter unique values right. For example, if you are on social media, we have mutual friends. Those mutual fronts are the negation of non-mutual friends i.e unique friends.

For that, we are using sets.

A set is a collection of well-defined data ie, non-empty, dissimilar, unique.

5. Comma Operator

The comma operator (,) evaluates each of its operands (from left to right) and returns the value of the last operand.

6. Swap values with array destructuring

Swapping values has never been easy like this, usually, we introduce a temporary variable then temporary = b; b = a; a = temporary;. But that produces a headache, right?

Well, now you can just swap using array destructuring:

7. Replace if true statements with &&

&& operators are less often used but now you will use more often.

We can make it shorter by using && operator:


We have looked into some lesser known tricks in JavaScript which can save you time and boost productivity:

  1. Swapping
  2. && conditional
  3. Random of Specific
  4. Adding/summing up an array
  5. Reducing array
  6. Comma operator
  7. Filtering Unique value

If you found this useful, be sure to like and share. What lesser known JavaScript tips do you use? Let me know in the comments!

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable frontends and backends with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and greatly speed up, scale, and standardize development as a team. Start with composable frontends like a Design System or Micro Frontends, or explore the composable backend. Give it a try →

Learn More



Adarsh gupta

Software Engineer | JavaScript developer | Technical Writer . Work with me? adarshguptaworks@gmail.com Connect with me? twitter.com/adarsh____gupta/