There are numerous ways to delete all items in an array. Some of them involve looping through an array and removing each item one by one, but I will show you one efficient way to accomplish this with one line of code.
In the following example, we are displaying all items in an array as well as the length of the array and upon click of a button, we are removing all items from the array which in turn will display nothing on the webpage and the length will become 0. Please have a look over the code example and steps given below.
HTML & CSS
We have 4 elements in the HTML file (div, button, h1, and ul). The div element is just a wrapper for the rest of the elements.
The inner text for the button element is “Remove” and for the h1 element is “Length”.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.