8

Some useful JavaScript One-Liners

 2 years ago
source link: https://dev.to/shshank/some-useful-javascript-one-liners-4eoe
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.
neoserver,ios ssh client
Cover image for Some useful JavaScript One-Liners
Shshank

Posted on Jul 7

• Updated on Jul 8

Some useful JavaScript One-Liners

JavaScript is one of the most powerful language in this modern world. In this article we will go through some of the useful JavaScript one-liners.

  • If you want to get the text that a user selects or highlights on a web page, there is a useful one-liner for that.
const getSelectedText = () => window.getSelection().toString();
console.log(getSelectedText);
  • There is a method called scrollTo(x,y), it allows you to scroll to a particular set of used coordinates.
const scrollToTop = () => window.scrollTo(0,0);
  • If you want to have a smooth scrolling animation, just do something like this:
const Top = () => window.scrollTo({top:0, behavior: 'smooth'});
  • Internet bandwidth is basically the amount of data is being transferred over an internet connection in a specified period of time.
navigator.connection.downlink;
  • If you want to redirect user to a specified location, you can do something like this:
const urlRedirect = url => location.href = url;
urlRedirect('https://dev.to/');

Hope you'll find some useful snippets in this code. Comment if you have more other than these.

Happy Coding Devs.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK