JS Coding Questions Logo
JS Coding Questions
#407💼 Interview💻 Code

What is the easiest way to resize an array

Advertisement

728x90

The length property of an array is useful to resize or empty an array quickly. Let's apply length property on number array to resize the number of elements from 5 to 2,

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

and the array can be emptied too

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

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 64

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
407of476