DEV Community

Discussion on: 3 ways to empty an Array in JavaScript

Collapse
 
lionelrowe profile image
lionel-rowe • Edited

Whoah, I never knew Array#length was settable!

Setting it almost seems like an antipattern, because you'd expect it to be a read-only property, but it also looks to be the cleanest way to empty an array in-place (the splice version is even less intuitive).

I guess this would be one example where a comment explaining "what" (rather than "why" or "how") would be useful.

// empties array
arr.length = 0
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sanchithasr profile image
Sanchithasr

Noted. I'll add the comment. It can be confusing.

Some comments have been hidden by the post's author - find out more