7 JavaScript Tricks You Should Know

7 JavaScript Tricks You Should Know

These tips became treasures when I found out about them

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:

Conclusion

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

[How We Build Micro Frontends
Building micro-frontends to speed up and scale our web development process.blog.bitsrc.io](https://blog.bitsrc.io/how-we-build-micro-front-ends-d3eeeac0acfc "blog.bitsrc.io/how-we-build-micro-front-end..")

[How we Build a Component Design System
Building a design system with components to standardize and scale our UI development process.blog.bitsrc.io](https://blog.bitsrc.io/how-we-build-our-design-system-15713a1f1833 "blog.bitsrc.io/how-we-build-our-design-syst..")

[The Composable Enterprise: A Guide
To deliver in 2022, the modern enterprise must become composable.blog.bitsrc.io](https://blog.bitsrc.io/the-composable-enterprise-a-guide-609443ae1282 "blog.bitsrc.io/the-composable-enterprise-a-..")

[7 Tools for Faster Frontend Development in 2022
Tools you should know to build modern Frontend applications faster, and have more fun.blog.bitsrc.io](https://blog.bitsrc.io/7-tools-for-faster-frontend-development-in-2022-43b6f663c607 "blog.bitsrc.io/7-tools-for-faster-frontend-..")