TypeScript Array join()

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

In this chapter, we will explore the join() method for arrays in TypeScript. This method is a built-in function that helps in concatenating all the elements of an array into a single string. Understanding how to use join() is useful for converting arrays to strings with a specified separator.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The join() method concatenates all the elements of an array into a single string. The elements are separated by a specified separator string. If no separator is provided, a comma (,) is used by default.

2. Syntax

array.join(separator?);

Parameters

  • separator (optional): A string to separate each pair of adjacent elements of the array. If omitted, the array elements are separated with a comma (,).

Return Value

The method returns a string with all array elements joined together, separated by the specified separator.

3. Examples

Let's look at some examples to understand how join() works in TypeScript.

Example 1: Basic Usage

In this example, we join all elements of an array into a single string with the default separator (comma).

let fruits: string[] = ["apple", "banana", "mango"];
let result = fruits.join();
console.log(result); // Output: "apple,banana,mango"

Example 2: Using a Custom Separator

In this example, we join all elements of an array into a single string with a custom separator.

let fruits: string[] = ["apple", "banana", "mango"];
let result = fruits.join(" - ");
console.log(result); // Output: "apple - banana - mango"

Example 3: Joining Numbers

In this example, we join all elements of a number array into a single string.

let numbers: number[] = [1, 2, 3, 4, 5];
let result = numbers.join(", ");
console.log(result); // Output: "1, 2, 3, 4, 5"

Example 4: Joining with No Separator

In this example, we join all elements of an array into a single string with no separator.

let letters: string[] = ["a", "b", "c"];
let result = letters.join("");
console.log(result); // Output: "abc"

Example 5: Joining an Array with Different Data Types

In this example, we join elements of an array that contains different data types into a single string.

let mixedArray: (number | string | boolean)[] = [1, "apple", true, 42, "banana"];
let result = mixedArray.join(", ");
console.log(result); // Output: "1, apple, true, 42, banana"

Example 6: Empty Array

In this example, we use join() on an empty array.

let emptyArray: any[] = [];
let result = emptyArray.join(", ");
console.log(result); // Output: ""

4. Conclusion

In this chapter, we explored the join() method for arrays in TypeScript, which is used to concatenate all the elements of an array into a single string with a specified separator. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use join() effectively can help in various array manipulation tasks in TypeScript, especially when converting arrays to strings for display or storage.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare