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
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
Here, we will create an array with some data, and then we will use the
splice() method to remove all elements of the array.
1let arr: Array<string> = ["Infinitbility", "aguidehub", "sortoutcode"];2arr.splice(0, arr.length);3console.log(arr);4// output: ""
I hope it helps you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.