

7 JavaScript Tricks You Should Know
source link: https://blog.bitsrc.io/7-javascript-tricks-you-should-know-435ab94c985f
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

7 JavaScript Tricks You Should Know
These tips became treasures when I found out about them
kraken images on unsplash.com
JavaScript is a popular programming language that is widely used for front-end web development, server-side scripting, and creating mobile and desktop applications.
It is a powerful language that offers many features and capabilities, including object-oriented programming, event-driven programming, and functional programming.
In this article, we will discuss seven useful JavaScript tricks that can help you write better code and improve your skills as a developer.
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 require random data from time to time. But sometimes we need to get random data from a specific dataset.
At that time, we can use the below snippet that will save you time:

4. Filter unique values
Sometimes we need to filter unique values correctly. For example, if you are on social media, you 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, and 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 the &&
operator:

Conclusion
We have looked into some lesser-known tricks in JavaScript that can save you time and boost productivity:
- Swapping
- && conditional
- Random of Specific
- Adding/summing up an array
- Reducing array
- Comma operator
- 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!
Thanks for reaching this far, you are a fantastic reader!
If you want to deep dive into CSS flexbox, check out this ebook:-
Complete Guide to CSS Flex and Grid
Become a Pro at building components & layouts with CSS Flexbox and Grid. Learn by looking at a curated list of 70+…
Follow me here Adarsh gupta as well as on Twitter.
Build apps with reusable components like Lego
Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:
→ Micro-Frontends
→ Design System
→ Code-Sharing and reuse
→ Monorepo
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK