Skip to content

re: 5 Way to Append Item to Array in JavaScript VIEW POST


Great article!

Something for beginners (I'm pretty sure you already knew about this):

One hacky way to add an empty item to an array (if we can even call it appending an item to an array, I think more appropriate term would be resizing) would be:

const array = [1, 2];

array.length = 3;

console.log(array); // [1, 2, <1 empty item>]

However you should never ever do this.


Yes! Good one, let me add it to my notes 💪 Thanks for sharing 😀


You can also use it to shrink array

const array = [1, 2]

array.length = 1

console.log(array) // [1]

WOOO! never thought of that! Adding to my notes!!! thanks for sharing 😁

code of conduct - report abuse