Top 5 one liners in JavaScript
<p>In the world of JavaScript, I’m the magician of one-liners, simplifying the complex with elegance.</p>
<h2>1.Copy to Clipboard</h2>
<p>In web apps, copy to clipboard is rapidly rising in popularity due to its convenience for the user.</p>
<pre>
const copyToClipboard = (text) =>
navigator.clipboard?.writeText && navigator.clipboard.writeText(text);
// Testing
copyToClipboard("Hello World!");
</pre>
<h2>2.Generate Random Color</h2>
<p>Does your application rely on random color generation? Look no further, the following snippet got you covered!</p>
<pre>
const generateRandomHexColor = () =>
`#${Math.floor(Math.random() * 0xffffff).toString(16)}`;
</pre>
<h2>3.Detect Dark Mode</h2>
<p>With the rising popularity of dark mode, it is ideal to switch your app to dark mode if the user has it enabled in their device. Luckily, media queries can be utilized for making the task a walk in the park.</p>
<pre>
const isDarkMode = () =>
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
// Testing
console.log(isDarkMode());
</pre>
<h2>4.Scroll To Top</h2>
<p>Beginners very often find themselves struggling with scrolling elements into view properly. The easiest way to scroll elements is to use the scrollIntoView method. Add behavior: "smooth" for a smooth scrolling animation.</p>
<p><a href="https://medium.com/@vishalkrishna8/top-5-one-liners-in-javascript-d9555bed7ece">Click Here</a></p>