JS Tricks

Shuffle array elements

By xyz on 2 weeks ago|0 comment||
0

We all require random data from time to time. But sometimes we need to get random data from a specific dataset.

At that time, we can use the below snippet that will save you time:

const array = [11, 12, 13, 14, 150, 15, 555, 556, 545]

console.log(array.sort(function() {
    return Math.random() - 0.5;
}));

// [13, 556, 14, 12, 545, 11, 150, 15, 555]

Add a Comment

Comments

Comments MIA, jokes needed ASAP. Add yours! 😄

What is JavaScript Hacks? 🚀

Hey there, fellow code adventurer! Ever wished JavaScript could be more than just lines of serious code? Well, welcome to JS Hacks – where JavaScript gets a playful makeover! 🎉

So, whether you're a coding newbie or a seasoned pro, join us on this epic quest to discover the quirkiest, coolest, and downright silliest JavaScript hacks out there. Trust us, your code will thank you (and maybe even crack a smile). 😄

Ready to hack, slash, and LOL your way through JavaScript? Let's dive in and unleash the fun-tastic power of JS Hacks together! 💻✨


DISCOVER

ENGAGE

Add Trick