Adding to an array
The push
method appends new items to the end of an array.
Loading TypeScript...
TypeScript will produce a compilation error if the new value does not match the array's element type.
Even though the program above is a valid JavaScript program that will successfully add "four"
to the array, TypeScript enforces the element type unless you
explicitly set the array's type to any[]
.
You can also use the push
method to append any number of new values with a single instruction.
Loading TypeScript...