JS - 4 : How to resize an array?

Mahabubur Rahman
0

How to resize an array?

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

let animals = ["Dog", "Cat", "Goat", "Horses"]
console.log(animals.length) // 4
console.log(animals) // ["Dog", "Cat", "Goat", "Horses"]

Now resize the array using length parameter as bellow

animals.length = 5

Now see what happend

console.log(animals.length) // 5
console.log(animals) //  ["Dog", "Cat", "Goat", "Horses", empty]

or 

animals.length = 3
console.log(animals.length) // 3
console.log(animals) //  ["Dog", "Cat", "Goat"]

now 

animals.length = 5
console.log(animals) // ["Dog", "Cat", "Goat", empty × 2]


Very nice and easy.

Post a Comment

0Comments
Post a Comment (0)