add an element to the end of an array in typescript

To add an element to the end of an array in TypeScript, you can use the push method. Here's how to use it:

index.ts
const myArray: number[] = [1, 2, 3];

myArray.push(4);

console.log(myArray); // Output: [1, 2, 3, 4]
102 chars
6 lines

In the example above, we have an array myArray with three elements. We then call the push method on it with the value 4 as an argument, which adds 4 to the end of the array. We then log the updated myArray to the console, which outputs [1, 2, 3, 4].

It's important to note that the push method mutates the original array, meaning it modifies the array in place rather than creating a copy of it.

gistlibby LogSnag