35+ Essential ES6 snippet for common problems (Gist link included)

Indepthcoder
5 min readSep 17, 2022

--

Here is the collection of JavaScript code snippet which are required in almost every project

Note : Collected Snippets from Stack Overflow and other website

Link to see snippets directly

Source code for all snippet can be found on this link

1. Get the base URL.

2. Check if the URL is absolute?

3. Get URL parameters as object?

4. Check if the element contains another element?

5. Get all the ancestors of the element?

6. Smooth-scroll element into view?

7. Handle click outside the element?

8. To generate UUID?

9. Get the selected text?

10. Copy text to the clipboard?

11. How to add styles to HTML element?

12. Toggle full-screen mode?

13. How to detect if Caps lock is on?

14. Check if the Date is valid?

15. Get colon time from Date?

16. Generate UNIX timestamp from Date?

17. Check the preferred language of the current user?

18. Check the preferred color scheme of the user?

19. Check if the device supports touch events?

20. Hide all elements specified ?

21. Check if the element has the specified class?

22. Toggle a class for an element?

23. Get the scroll position of the current page?

24. smooth-scroll to the top of the page?

25. Check if the element specified is visible in the viewport?

26. Fetch all images within an element?

27. figure out if the device is a mobile device or a desktop/laptop?

28. Encode a set of form elements as an object?

29. Retrieve a set of properties indicated by the given selectors from an object ?

30. Invoke the provided function after wait (in milliseconds)?

31. How to trigger a specific event on a given element, optionally passing custom data?

32. How to remove an event listener from an element?

33. Get readable format of the given number of milliseconds?

34. Get the difference (in days) between two dates?

35. How to make a GET request to the passed URL?

36. Make a POST request to the passed URL

37. Find out if the browser tab of the page is focused?

Hope you find the article important, I have attached the link of github to copy paste snippet . Do Appreciate

--

--

Indepthcoder

Programming enthusiast and JavaScript lover😍 years of happiness😊 Occasionally, I like to express my thoughts into articles🧠