There are many ways to empty an array in Typescript, like using the splice method or doing it manually. So how to do it? Let’s go into detail now.
Empty an array in TypeScript
Use splice method
The splice will change your origin array by removing elements, and it can also add the element.
Syntax:
array.splice(start, remove amount, add items)
Parameters:
- start: The start index that you want to remove the element.
- remove amount: The number of elements you want to remove from your array.
- add items: All items you want to add to your array replace the removed one. So that if you pass in only the start position is 0, then all elements in the array will be removed.
Example:
const nameArray: string[] = [ "Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel", ]; nameArray.splice(0) console.log(nameArray);
Output:
[LOG]: []
As you see here, I have removed all elements in the origin array with the splice method. If you don’t want to change in origin array, you can clone a new array and then work with it.
Example:
const nameArray: string[] = [ "Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel", ]; // Clone the new array const newArr = Object.assign([], nameArray); // Empty array newArr.splice(0); console.log(newArr); console.log(nameArray);
Output:
[LOG]: []
[LOG]: ["Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel"]
Assign to empty array
You can reassign an array to an empty one, but it might make you lose data, so you should store the value somewhere.
Example:
let nameArray: string[] = [ "Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel", ]; // Clone the new array const newArr = Object.assign([], nameArray); // Assign to an empty array nameArray = []; console.log(newArr); console.log(nameArray);
Output:
[LOG]: ["Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel"]
[LOG]: []
If you set the length array to 0, your array also becomes empty.
Example:
let nameArray: string[] = [ "Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel", ]; // Set the length array to 0 nameArray.length = 0; console.log(nameArray);
Output:
[LOG]: []
Use pop method
The pop method will help you remove the last element in your array so that you can use that pop method in one loop based on your array length.
Syntax:
array.pop()
Example:
let nameArray: string[] = [ "Tommy", "Jonmma", "Alex", "Anna", "Antony", "Hallan", "Michel", ]; // Use the while loop and pop() method to empty the array while (nameArray.length) { nameArray.pop(); } console.log(nameArray);
Output:
[LOG]: []
Here with a while loop, the loop will continue until the condition return false, which here is 0.
You can read more about array type here.
Summary
In this article, I showed you how to empty an array in TypeScript. I recommend you use the splice method or simply assign it to an empty array if you have no special condition.
Maybe you are interested:
- Declare an Empty Array for a typed Variable in TypeScript
- How to find an Object in an Array in TypeScript
- How To Define An Array With Multiple Types In TypeScript

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm