25 Killer JavaScript One-Liners That’ll Make You Look Like a Pro
<p>A list of one-liners you should know to up your knowledge of JavaScript.</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/0*V3fXz2tXf6l7mrCO" style="height:525px; width:700px" /></p>
<p>Photo by <a href="https://unsplash.com/@grakozy?utm_source=medium&utm_medium=referral" rel="noopener ugc nofollow" target="_blank">Greg Rakozy</a> on <a href="https://unsplash.com/?utm_source=medium&utm_medium=referral" rel="noopener ugc nofollow" target="_blank">Unsplash</a></p>
<h1>1.# Copy content to the clipboard</h1>
<p>In order to improve the user experience of the website, we often need to copy the content to the clipboard, so that users can paste it to the designated place.</p>
<pre>
const copyToClipboard = (content) => navigator.clipboard.writeText(content)
copyToClipboard("Hello fatfish")</pre>
<h1>2.# Get the mouse selection</h1>
<p>Have you encountered this kind of situation before?</p>
<p>We need to get the content selected by the user.</p>
<pre>
const getSelectedText = () => window.getSelection().toString()
getSelectedText()</pre>
<h1>3.# Shuffle an array</h1>
<p>Shuffle an array? This is very common in lottery programs, but it’s not truly random.</p>
<pre>
const shuffleArray = array => array.sort(() => Math.random() - 0.5)
shuffleArray([ 1, 2,3,4, -1, 0 ]) // [3, 1, 0, 2, 4, -1]</pre>
<h1>4.# Convert rgba to hexadecimal</h1>
<p>We can convert the rgba and hexadecimal color values to each other.</p>
<p><a href="https://javascript.plainenglish.io/25-killer-javascript-one-liners-thatll-make-you-look-like-a-pro-d43f08529404">Visit Now</a></p>