How to clear JavaScript array

Given a JavaScript array, see how to clear it and empty all elements

There are multiple ways to clear the JavaScript array.

The easiest way is to set its length to 0:

const list = ['a', 'b', 'c']
list.length = 0

Another method is to mutate the original array reference and assign an empty array to the original variable, so you need to useletinsteadconst:

let list = ['a', 'b', 'c']
list = []

Download mine for freeJavaScript beginner's manual


More js tutorials: