Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To empty an array in typescript, use the splice() method with array length it will delete all elements of the array without compromise with reference, and also it is one of the fastest ways to empty an array.

The splice() method takes the index (from which the splicing should start ) and the number of items to be removed as parameters and splices the elements.

We have to pass the 0 as an index (the first element) and the length of the array as parameters that ends up emptying the whole array. The performance of this method is almost as fast as assigning the new array method

array.splice(0, array.length)

Today, I’m going to show you How do I empty an array in typescript, as above mentioned, I’m going to use the above-mentioned splice() method to empty the array.

Let’s start today’s tutorial how do you empty an array in typescript?

Typescript empty an array example using splice() method

Here, we will create an array with some data, and then we will use the splice() method to remove all elements of the array.

let arr: Array<string> = ["Infinitbility", "aguidehub", "sortoutcode"];
arr.splice(0, arr.length);
console.log(arr);
// output: "[]"

I hope it helps you, All the best đź‘Ť.