JS Tricks

Using length to delete empty in an array

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

This technique will help you resize and empty an array.

To delete n elements in an Array, use array.length.

var array = [1, 2, 3, 4, 5, 6]; 
console.log(array.length); // 6 
array.length = 3; 
console.log(array.length); // 3 
console.log(array); // [1,2,3]

var array2 = [1, 2, 3, 4, 5, 6]; 
array2.length = 0; 
console.log(array2.length); // 0 
console.log(array2); // []

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