October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Add Elements to an Array in TypeScript

Use push() to append to a mutable TypeScript array, unshift() to add at the front, or spread and concat() to return a new array without changing the source.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use push() to add values to the end of a mutable TypeScript array, or unshift() to add them at the beginning. If you need to keep the original array unchanged—or your input is typed as readonly—create a new array with spread syntax or concat().

Append elements to the end with push()

push() adds one or more values to the end of the existing array. It changes that array and returns its new length, not a replacement array.

const names: string[] = ["Ada", "Lin"];
names.push("Grace");
// names is now ["Ada", "Lin", "Grace"]

const newLength = names.push("Edsger");

The Handbook describes the method signature as push(...items: Type[]): number; the return value is the array’s new length. TypeScript Handbook: Interfaces

Add several values

Pass each value as a separate argument:

names.push("Katherine", "Alan");

To append the contents of another array, spread its elements into the call:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const moreNames: string[] = ["Katherine", "Alan"];
names.push(...moreNames);

For function-call spread, TypeScript must be able to type the arguments. A tuple such as one inferred with as const preserves fixed positions; the Handbook also documents rest parameters as a relevant pattern. If your project targets older runtimes, check its TypeScript configuration: supporting iteration during downlevel compilation can require downlevelIteration. TypeScript Handbook: More on Functions

Insert elements at the beginning with unshift()

Call unshift() to add values before the current first element. The existing items remain after the inserted values, and the array is mutated.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
const names: string[] = ["Ada", "Lin"];
names.unshift("Barbara");
// names is now ["Barbara", "Ada", "Lin"]

MDN: Array.prototype.push() describes the related array operations, including unshift().

Create an appended array without changing the original

If another part of your program should continue to see the original contents, build a new array instead of calling a mutating method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const original = [1, 2, 3];
const withFour = [...original, 4];
const alsoWithFour = original.concat(4);

Both examples produce a new array and leave original intact. Use spread for a concise expression, or concat() when it better fits the surrounding code. To combine two arrays, use [...first, ...second] or first.concat(second).

Choose the method that matches your intent

Need Approach Effect
Append to a mutable array array.push(value) Changes the existing array and returns its new length.
Add at the beginning array.unshift(value) Changes the existing array by inserting at the front.
Append while keeping the source unchanged [...array, value] or array.concat(value) Creates a new array.
Combine arrays into a new result [...first, ...second] or first.concat(second) Creates an array containing the elements from both inputs. TypeScript 4.0 documented typed variadic tuple concatenation for preserving tuple structure in applicable cases.
Accept an array callers may share without mutating it readonly T[] or ReadonlyArray<T> Prevents mutating methods from being called through that TypeScript reference.

The TypeScript 4.0 release notes explain variadic tuple types and concatenation: TypeScript 4.0 release notes.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Append to a readonly input by returning a new array

A function can accept a readonly array to make clear that it will not mutate the caller’s input, then return a mutable array with the additional item:

function withItem<T>(items: readonly T[], item: T): T[] {
  return [...items, item];
}

TypeScript’s readonly array type removes mutating methods such as push(). It also does not assign to a mutable array variable without an assertion. Prefer constructing a fresh array, as above, instead of asserting away the readonly constraint. The Handbook describes ReadonlyArray as “a special type that describes arrays that shouldn’t be changed.” TypeScript Handbook: Object Types

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What readonly does—and does not—guarantee

readonly is a TypeScript compile-time constraint, not a runtime freeze. It prevents mutation through a reference typed as readonly, but it does not stop another alias to the same JavaScript array from changing its contents. If the program requires runtime immutability, it needs a separate runtime design.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.