7 JavaScript One-Liners that you should know

Today we will share the 7 useful JavaScript One-Liners that you should know.

7 JavaScript One-Liners

  1. Check if the current user has touch events supported
  2. Copy text to clipboard
  3. Reverse a string
  4. Check if the current tab is in view/focus
  5. Scroll to top of the page
  6. Generate a random string
  7. Check if the current user is on an Apple device

1. Check if the current user has touch events supported

Use the following function to check the touch support.

2. Copy text to clipboard

Use the navigator to copy text to the clipboard.

Suggested article: How to copy text to the clipboard using JavaScript

3. Reverse a string

Use the single line of code using split(), reverse() & join() methods to reverse a string.

4. Check if the current tab is in view/focus

We can check if the current tab is in view/focus by using the document.hidden property.

5. Scroll to top of the page

Use the window.scrollTo() method to change the scroll position.

Suggested article: Animated sticky header on scroll using JavaScript

6. Generate a random string

Using the Math.random(), we can generate a random string.

Suggested article: Generate a random password using JavaScript

7. Check if the current user is on an Apple device

We can use navigator.platform to check if the current user is on an Apple device.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *