DEV Community

Cover image for Killer JavaScript One Liners
Shshank
Shshank

Posted on

Killer JavaScript One Liners

Some useful JavaScript One-Liners

After a good response from the community, I decided to post some few more JavaScript one liners, that might help you a lot. I have posted a link to my previous article of Some useful JavaScript One Liners

Clear All Cookies

const clearCookies = document.cookie.split(';').forEach((cookie) => (document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`)));
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

Reverse a String

You can reverse a string in one line using split, join and reverse methods.

const strReverse = str => str.split('').reverse().join('');
strReverse('Shshank');
// knahshS
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

Generate a Random Hex

This method generates a random hex code using Math.random() and padEnd().

const hexClr = () => '#' + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0');
console.log(hexClr());
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

Capitalise a String

Javascript doesn't have an inbuilt capitalise function, so we can use the following code of the purpose.

let str = 'follow Shshank for amazing posts';
let capStr = str.replace(/\w\S*/g, (w) => (w.replace(/^\w/, (c) => c.toUpperCase())));
console.log(capStr);
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

Copy to Clipboard

Easily copy any text to clipboard using navigator.clipboard.writeText.

const copy = (text) => navigator.clipboard.writeText(text);
copy('Shshank');
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

Hope you like this post. If you find this post useful, please like and share with fellow developers. Follow me for more such posts.

Thank you.

Top comments (17)

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
frankwisniewski profile image
Frank Wisniewski •

seen 1000 times and wrong 1000 times...

const str = 'Hello 😊 World';
const reversedStr = str.split('').reverse().join('');
console.log(reversedStr); // 'dlroW �� olleH'
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
ecyrbe profile image
ecyrbe •

You should then post the right one :

const str = 'Hello 😊 World';
const reversedStr = [...str].reverse().join('');
console.log(reversedStr); // 'dlroW 😊 olleH'
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
frankwisniewski profile image
Frank Wisniewski •

You too ....

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini Thread
 
ecyrbe profile image
ecyrbe •

You made a critique without even trying to help. That was my point.
If you have knowledge, you should share it.

Thanks Luke for taking the time to add a fool proof version.

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini Thread
 
frankwisniewski profile image
Frank Wisniewski •

this has been chewed through here so many times, a simple search is enough... dev.to/ianfelix/how-to-invert-a-st...

 
adam_cyclones profile image
Adam Crockett 🌀 •

You could also hard code the string backwards 😉

const string = "olleh"
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

That's the efficiency of build time.

Nice one Luke

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini Thread
 
jankapunkt profile image
Jan Küster 🔥 •

Luke you should make this comment a new article do we can bookmark it 🙃

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini Thread
 
adam_cyclones profile image
Adam Crockett 🌀 •

Oh man an article on hard coding strings where could that lead 🤪

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini Thread
 
bias profile image
Tobias Nickel •

at least it does not lead to a configuration or environment variable called empty_string.

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini Thread
 
adam_cyclones profile image
Adam Crockett 🌀 •

I don't know what that is 👍

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
joolsmcfly profile image
Julien Dephix • • Edited

Clear all cookies is more of a tiny program than a one-liner ;)

To capitalise a string you could use the same technique as to reverse a string.

const upper = s.split("").map(n => n.toUpperCase()).join('');
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini

I find it easier to read / understand than two replace.
Hope this helps.

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
frankwisniewski profile image
Frank Wisniewski • • Edited

you have to understand the task...

s='this is a mistake';
const upper = s.split("").map(n => n.toUpperCase()).join('');
console.log(upper === s.toUpperCase()) // true
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
joolsmcfly profile image
Julien Dephix •

Ha, my bad! I read too quickly.

I guess I should not be reading code on my phone at 11pm. 😓

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
jonrandy profile image
Jon Randy 🎖️ • • Edited

Your random hex function has a number of problems. For starters, it will never return #ffffff. Also there are many other values it will never return as you're using padEnd instead of padStart

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
jonrandy profile image
Jon Randy 🎖️ • • Edited
const hexClr = ()=>'#'+(~~(Math.random()*8**8)).toString(16).padStart(6,0)
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
feba33 profile image
feba •

I personally use this to capitalize:
str[0].toUpperCase() + str.substr(1)

⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini
 
prajyu profile image
prajyu •

How about adding another one liner
To remove duplicate values

const uniqueValues= (arr) => [...new Set(arr)];
⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini ⚽ Live Match: shshank/killer javascript one liners ii - Pertandingan Hari Ini