DEV Community

Cover image for JavaScript One-Liners That Actually Slap 🚀 (And One That Doesn't 👀)
Aaron Rose
Aaron Rose

Posted on

JavaScript One-Liners That Actually Slap 🚀 (And One That Doesn't 👀)

Y’all ever write a line of code so clean, so efficient, so unnecessarily cool that you just sit back and stare at it? 😮‍💨 That’s the vibe we’re bringing today. These JavaScript one-liners aren’t just efficient—they straight-up slap.

But wait! My awesome friend Gemini (yes, the AI) read this and dropped some knowledge. So we're making it even better: keeping the fun, fixing the flaws. Let’s get into it.


1. The “Did They See My Text?” Scroll Detect

const isAtBottom = () => 
  window.innerHeight + window.scrollY >= document.body.offsetHeight;
Enter fullscreen mode Exit fullscreen mode

When you need to know if they’ve scrolled all the way down to your 4AM novel-text.

Perfect for infinite scroll, “back to top” buttons, or just knowing your user is fully invested. No more awkward “hello??” moments.


2. The Main Character RGB Vibe 🌈

const randomRGB = () => 
  `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)})`;
Enter fullscreen mode Exit fullscreen mode

For when your website needs that main character energy.

Gemini pointed out that while the | 0 trick is cool, Math.floor() is clearer for beginners. So we're keeping it clean and iconic. Use this for dynamic backgrounds, user themes, or just to annoy your designer. You do you.


3. The “I’m So Random” Array Shuffle (But Better)

// 🚨 The Old Way (it's biased, don't @ me)
// const shuffle = arr => arr.sort(() => Math.random() - 0.5);

// ✅ The Correct Way (Fisher-Yates for the win)
const shuffle = arr => {
  for (let i = arr.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [arr[i], arr[j]] = [arr[j], arr[i]];
  }
  return arr;
};
Enter fullscreen mode Exit fullscreen mode

Shuffle your array like you shuffle your Spotify playlist after a heartbreak—but fairly.

Gemini called me out! The classic sort(() => Math.random() - 0.5) is biased. So here’s the Fisher-Yates algorithm—the industry-standard, truly random way to shuffle. It’s a few more lines, but your code deserves to be right. Your users deserve fairness, even in chaos.


4. The “Are You Even Online?” Check

const isOnline = () => navigator?.onLine;
Enter fullscreen mode Exit fullscreen mode

Are they online or are they ghosting?

Use this to toggle UI states, sync data when back online, or just call people out (respectfully). Because sometimes it’s Wi-Fi, sometimes it’s not.


5. The “This Isn’t a Phase, Mom” Dark Mode Toggle 🌙

const toggleDarkMode = () => document.documentElement.classList.toggle('dark');
Enter fullscreen mode Exit fullscreen mode

For when light mode is just too much before noon.

Pair this with CSS variables and you’ve got a vibe switch that’s smoother than your comeback. Your eyes will thank you.


BONUS: The “Copy That, Sis” Clipboard One-Liner

const copyToClipboard = async text => await navigator.clipboard.writeText(text);
Enter fullscreen mode Exit fullscreen mode

When you need to copy that error message, token, or meme URL real quick.

No more CTRL+C / CTRL+V gymnastics. You’re better than that.


Go Forth and Slap (Responsibly) 👏

There you have it. Five (plus a bonus) JavaScript one-liners that don’t just work—they slap, correctly. Shoutout to Gemini for keeping us technically sharp! 💎

What’s your favorite one-liner that actually slaps? Drop it in the comments—let's learn together.

Keep your code clean and your vibes cleaner. ✨


Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of The Rose Theory series on math and physics.

Top comments (0)