How to Use the TypeScript forEach Method on Arrays

TypeScript forEach method: Command Prompt output listing orders with their index and total revenue

The TypeScript forEach method calls a function once for every element of an array: orders.forEach((order, index) => { … }). TypeScript infers the element type for the callback, and forEach always returns void, so use it for side effects such as logging or totals: My setup for these examples: TypeScript 7.0.2 and Node.js v22.22.2, compiled … Read more >>

How to Get Distinct Values from an Array in TypeScript

TypeScript distinct values: Command Prompt output of new Set removing repeated state codes from an array

To get distinct values from an array in TypeScript, pass the array to a Set and spread it back: […new Set(arr)]. A Set keeps each value once, in first-seen order, and TypeScript keeps the element type, so a string[] stays a string[]: I compiled these snippets with TypeScript 7.0.2 running on Node.js v22.22.2. One of … Read more >>

How to Use TypeScript Const Arrays?

Const Array in TypeScript

In this tutorial, I will explain how to use the const arrays in TypeScript. I will also show you some examples of using const arrays in TypeScript. What is a Const Array in TypeScript? Let me first explain what a const array is in TypeScript. The const keyword in TypeScript is used to declare variables … Read more >>

How to Use the TypeScript Array splice() Method

TypeScript array splice: Command Prompt output removing, inserting and replacing route stops with splice

The TypeScript array splice() method changes an array in place: arr.splice(start, deleteCount, …items) removes deleteCount elements at start, inserts any items there, and returns the removed elements as a new array. Use toSpliced() when you need a copy instead: My test bench was TypeScript 7.0.2 with Node v22.22.2, strict mode on, and the printed arrays … Read more >>

How to Convert an Enum to an Array in TypeScript

TypeScript enum to array: Command Prompt output of a generic helper converting numeric, string and mixed enums

The quickest TypeScript enum to array conversion is a single call: pass the enum to Object.values() for its values or Object.keys() for its names. A string enum gives you a clean array right away; a numeric enum needs one filter() first: My setup for this page was TypeScript 7.0.2 and Node.js v22.22.2 with strict mode … Read more >>

How to Convert an Object to an Array in TypeScript

TypeScript convert object to array: Command Prompt table of store objects built with Object.entries and map

To convert an object to an array in TypeScript, use Object.values(obj) for the values, Object.keys(obj) for the property names, or Object.entries(obj) for [key, value] pairs. Each returns a new array and leaves the object alone: These examples were compiled with –strict and run with TypeScript 7.0.2 on Node.js v22.22.2; nothing below is hand-typed output. You’ll … Read more >>

How to Convert an Array to a String in TypeScript?

Convert an Array to a String in TypeScript

If you are a TypeScript developer working with arrays, this tutorial will be very helpful. In it, I explained how to convert an array to a string in TypeScript. Let’s understand why you might need to convert an array to a string. Arrays are versatile data structures that can hold multiple values, but sometimes you … Read more >>

How to Convert a String to an Array in TypeScript

TypeScript convert string to array: Command Prompt output comparing split and Array.from on a string with emoji

To convert a string to an array in TypeScript, use split() with a separator, such as “a,b,c”.split(“,”), which returns a string[]. For an array of single characters, use Array.from(str) instead of split(“”): Here I’m compiling in strict mode with TypeScript 7.0.2 with Node.js v22.22.2, and each output block is pasted from the console. You’ll see … Read more >>

How to Convert a Set to an Array in TypeScript

Convert a Set to an array in TypeScript: Command Prompt output of turning a Set of orders into a sorted array

Spread syntax is the quickest way to convert a Set to an array in TypeScript: […mySet]. Array.from(mySet) does the same job. Both return a new, typed array in insertion order, so a Set<string> becomes a string[]: I compiled and ran every example below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, and the outputs … Read more >>

How to Use Join() Method to Combine Array of Strings in TypeScript?

Join() Method to Combine Array of Strings in TypeScript

In this tutorial, I will explain how to use TypeScript’s join() method to combine an array of strings into a single string. This method is particularly useful when you need to create a comma-separated list, format strings for display, or generate URLs from an array of parameters. What Is The Join() Method in TypeScript The … Read more >>