10 Awesome JavaScript One-Liners

10 Awesome JavaScript One-Liners


Published at - Nov 23, 2021

Good day, everyone! I have 5 years of experience as a full-stack web developer. I’d like to share some mind-blowing JavaScript one-liners with you today.

Awesome JavaScript One-Liners: We can split a string. As a result, we’ll have an array. We can revert this array and join to get a string.

let str = "codingcafe";// output
str = str.split("").reverse().join("");
efacgnidoc 
console.log(str);

Awesome JavaScript One-Liners: To obtain query parameters, we must first divide the URL at “?” and then replace “=” with “:” and “&” with “,”. As we did here-

const getQueryParams = (URL) => JSON.parse(// output
 '{"' +
 decodeURI(URL.split('?')[1])
{name: 'gyan', age: '24'} 
 .replace(/&/g, '","')
 .replace(/=/g, '":"') +
 '"}'
 ); getQueryParams('https://codingcafe.co.in?name=gyan&age=24');

Awesome JavaScript One-Liners: The remainder becomes 0 when we divide an even integer by two. Otherwise, it’s an odd number.

const isEven = (num) => num % 2 === 0 ? true : false; isEven(5) // //output - false

Awesome JavaScript One-Liners: To copy a text, we can use a JavaScript navigator.

const copy = (text) => navigator.clipboard.writeText('Hello world!');

To paste text:

const text = navigator.clipboard.readText();

Awesome JavaScript One-Liners: We can make a set from an array to get rid of duplicates.

const removeDuplicates = (ary) =>{
 return [...new Set(ary)]
} removeDuplicates([5,6,1,2,3,6,3,5,1]) // [5, 6, 1, 2, 3]

Awesome JavaScript One-Liners: We can use array.sort() with ( Math.random() — 0.5). Math.random() — 0.5 is a random number, which may be positive or negative.

const shuffle =(array) => array.sort(() => Math.random() - 0.5); shuffle([3,7,6,5])
// [7, 5, 3, 6]

Awesome JavaScript One-Liners: we can document.hidden to check-

const inView = () => document.hidden;
inView();
*// Result: returns true or false depending on if tab is focused*

Awesome JavaScript One-Liners: we can document.activeElement to check-

const inView = (*el*) => (el === document.activeElement);
inView(element);
*// Result: returns true or false depending on if element is focused*

Awesome JavaScript One-Liners: The x- and y-coordinates to scroll to will be sent to the window.scrollTo() function. We’ll scroll to the top of the page if we set these to zero and zero.

const scrollToTop = () => window.scrollTo(0,0);
scrollToTop()

Awesome JavaScript One-Liners: The x- and y-coordinates to scroll to will be sent to the window.scrollTo() function. We’ll scroll to the bottom of the page if we set these to zero and the height of the page.

const scrollToBottom = () => window.scrollTo(0,document.body.scrollHeight);
scrollToBottom()

Thank you for reading this article. I hope you like this article, don’t forget to clap 👏 to appreciate my work and follow me for most posts like this.





About author

Harendra
Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.



Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile