How to empty an array in JavaScript?

coderguy asked this question 8 months ago
coderguy on Dec 2, 2021

Need a way to take an array of strings:

["string", "string", "string"]

And convert it into an empty array:

3 suggested answers
itsbambi on Dec 8, 2021
array = []

This sets the array to a new empty version.

Be careful with this method if you have a different variable that references the original array. If that's the case, that variable will still hold the contents of the unchained array.

0 replies
nick on Dec 8, 2021
array.length = 0

This clears the array by setting its length property to 0.

0 replies
looper003 on Dec 8, 2021
array.splice(0, array.length)

This will clear your array!

0 replies