Sometimes, we want to limit the length of an array in JavaScript.
In this article, we’ll look at how to limit the length of an array in JavaScript.
How to limit the length of an array in JavaScript?
To limit the length of an array in JavaScript, we can set the length
property to the max length of the array.
For instance, we write
arr.length = Math.min(arr.length, 5);
to set arr.length
to the minimum number between arr.length
and 5.
Then arr
can’t have length more than 5.
Conclusion
To limit the length of an array in JavaScript, we can set the length
property to the max length of the array.