Resize Javascript Array Size

engrmahabub
Mahabubur Rahman
Published on Dec, 15 2023 1 min read 0 comments
image

How to resize an array?

We can resize an array very simple way. See the following example - 

let animals = ["Dog", "Cat", "Goat", "Horses"]

animals.length
4

animals
['Dog', 'Cat', 'Goat', 'Horses']

Now resize the array using length parameter as bellow

animals.length = 5

Now see what happened

animals.length
5
animals
(5) ['Dog', 'Cat', 'Goat', 'Horses', empty]

Now try as bellow - 

animals.length=3
3
animals.length
3
animals
(3) ['Dog', 'Cat', 'Goat']

Now again

animals.length = 5
5
animals
(5) ['Dog', 'Cat', 'Goat', empty × 2]

 

 

 

 

 

 

 

0 Comments