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.
You may also like to read: Killer JavaScript One Liners
Happy Coding Devs.











Small typo in your smooth scrolling example, missing
'at the end of smooth. Instead of your urlRedirect function, you can directly calllocation.assign(url).