InterviewHubPRO
Section Overview
Q411

Question 411 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

79%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

What is the easiest way to resize an array

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
 var array = [1, 2, 3, 4, 5];
 console.log(array.length); // 5

 array.length = 2;
 console.log(array.length); // 2
 console.log(array); // [1,2]

and the array can be emptied too

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