Savio Profile picture
17 y/o — building a few Saas apps in public. sold https://t.co/K5Tq4ULrpz (2023) & 5x hackathon winner. Maker of the year 2021 @ProductHunt.

Sep 20, 2021, 12 tweets

10 Killer JavaScript Snippets for Web Developers 🔥

One line JavaScript snippets to boost up your productivity. 98.8% people don't know all of them!

A thread 🧵👇️

1️⃣ Capitalise a String

Javascript doesn't have an inbuilt capitalise function, so we can combine charAt and toUpperCase methods for that.

2️⃣ Remove Duplicates from Array

You can easily remove duplicates with Set in JavaScript. Its a life saver.

3️⃣ Find Average of Given Numbers

Find the average between given numbers using reduce method in JavaScript.

4️⃣ Scroll to Top

You can use window.scrollTo(0, 0) method to automatic scroll to top. Set both x and y as 0.

5️⃣ Reverse a string

Combining split, reverse and join methods could create a reverse effect of a string.

6️⃣ Shuffle an Array

Shuffling an array made super easy with sort and random methods.

7️⃣ Detect Dark Mode

Check if a user's device is in dark mode by using "(prefers-color-scheme: dark)" method.

8️⃣ Generate Random Hex Color

You can generate random hex colors with Math.random and padEnd properties.

9️⃣ Check if Date is Valid

Use the following snippet to check if a given date is valid or not.

🔟 Copy to Clipboard

Easily copy any text to clipboard using the navigator.clipboard.writeText property of JavaScript.

If you found this thread helpful, please consider following me @saviomartin7 and retweeting the first tweet.

Thank you so much for your support, friends! 🙏

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling