Table of Contents

JavaScript Array join() Method is used to combine all elements of an array into a single string. It takes the array elements and connects them using a specified separator. If no separator is provided, JavaScript uses a comma (,) by default.

The join() method is useful when you need to convert an array into readable text, display a list of values, create comma-separated data, or generate formatted strings dynamically.

For example, suppose you have an array containing several programming languages:

const languages = ["JavaScript", "Python", "PHP", "Java"];

const result = languages.join();

console.log(result);

Output:

JavaScript,Python,PHP,Java

The join() method combines all array elements into one string, separating them with commas.

You can also specify a custom separator, such as a space, hyphen, or pipe symbol.

const languages = ["JavaScript", "Python", "PHP", "Java"];

const result = languages.join(" - ");

console.log(result);

Output:

JavaScript - Python - PHP - Java

In this tutorial, you will learn how the JavaScript join() method works, its syntax, separator options, behavior with different data types, and practical coding examples.

Syntax and Formal Specification

The method signature is join(separator).

Parameters

  • separator (optional) — A string used to separate each pair of adjacent elements. If omitted, elements are separated by a comma (,). If provided as an empty string (""), elements are concatenated with no characters between them .

Return Value

A string containing all array elements joined. If array.length is 0, an empty string is returned .

The toString Connection

Array.prototype.toString() internally calls join() with no arguments. Overriding join on an array instance also overrides its toString behavior . This means customizing join changes how the array appears when coerced to a string (e.g., in template literals or string concatenation).

javascript
const arr = [1, 2, 3];
arr.join = function () { return "custom"; };
console.log(`${arr}`);   // "custom" (toString delegates to join)

null and undefined Become Empty Strings

Unlike most string coercion in JavaScript, join() treats null and undefined as empty strings, not as "null" or "undefined" . This produces consecutive separators where such values appear.

javascript
console.log([1, null, 3].join("-"));        // "1--3"
console.log([1, undefined, 3].join("-"));   // "1--3"
console.log([1, , 3].join("-"));            // "1--3" (sparse hole)

Sparse Arrays Produce Extra Separators

join() iterates empty slots (holes) as if they were undefined, meaning holes contribute an empty string and therefore produce extra separators . This is distinct from methods like map() or filter(), which skip holes entirely.

javascript
const sparse = [1, , 3];   // eslint-disable-line no-sparse-arrays
console.log(sparse.join(","));          // "1,,3"
console.log(sparse.join());             // "1,,3"
console.log([1, undefined, 3].join());  // "1,,3" (indistinguishable)

The result of joining a sparse array is indistinguishable from joining an array with explicit undefined at the same positions.

Nested Arrays: Recursive Stringification

join() recursively converts nested arrays to strings. Because each inner array’s toString() (which is join() with no arguments) uses commas as its default separator, nested arrays appear flattened in the output .

Critically, the separator argument only controls the first level. Deeper levels always use the default comma.

javascript
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

console.log(matrix.join());       // "1,2,3,4,5,6,7,8,9"
console.log(matrix.join(";"));    // "1,2,3;4,5,6;7,8,9"

The inner arrays are joined with commas regardless of the outer separator. To control inner separators, map the inner arrays first:

javascript
console.log(matrix.map(row => row.join("-")).join(";"));   // "1-2-3;4-5-6;7-8-9"

join() vs String Concatenation in a Loop

For building strings from many pieces, join() is significantly faster than repeated += concatenation in older engines, though modern JIT compilers have narrowed the gap. The join() approach is also clearer and avoids intermediate string allocations.

javascript
const parts = ["a", "b", "c", "d"];

// Idiomatic and fast
const joined = parts.join("");

// Less idiomatic (though modern engines optimize this well)
let concatenated = "";
for (const part of parts) concatenated += part;

How JavaScript Converts Array Elements During join()

Before combining array elements, the join() method converts each element into a string. This conversion allows arrays containing different data types to produce a single string.

For primitive values, JavaScript uses their string representation.

const values = [100, true, false, "JavaScript"];

const result = values.join(" | ");

console.log(result);

Output:

100 | true | false | JavaScript

Here, the number 100 becomes "100", while the Boolean values become "true" and "false".

Objects behave differently. When a plain object is converted to a string, its default representation is usually "[object Object]".

const data = [
    "JavaScript",
    100,
    { name: "Rahim" },
    true
];

console.log(data.join(" - "));

Output:

JavaScript - 100 - [object Object] - true

If you want to display a particular object property, transform the array before calling join().

const users = [
    { name: "Rahim", age: 25 },
    { name: "Karim", age: 28 },
    { name: "John", age: 30 }
];

const names = users
    .map(user => user.name)
    .join(", ");

console.log(names);

Output:

Rahim, Karim, John

The map() method extracts the required property, and join() combines the resulting names into one string.

What Happens When join() Is Called on a Cyclic Array?

A cyclic array is an array that contains a reference to itself, either directly or through another object.

JavaScript handles a direct self-reference specially during array string conversion. When an array encounters itself during join(), that recursive reference contributes an empty string.

const numbers = [10, 20];

numbers.push(numbers);

console.log(numbers.join(" - "));

Output:

10 - 20 -

The array contains its own reference as the third element. JavaScript does not recursively expand that reference forever. Instead, the recursive occurrence contributes an empty string.

This is an unusual edge case, but it demonstrates why arrays with circular references should be handled carefully when generating strings.

Using join() with a Custom Array-Like Object

The join() method is generic, meaning it does not require its this value to be an actual array.

It only needs a suitable length property and indexed properties.

const languages = {
    0: "JavaScript",
    1: "Python",
    2: "PHP",
    3: "Java",
    length: 4
};

const result = Array.prototype.join.call(
    languages,
    " | "
);

console.log(result);

Output:

JavaScript | Python | PHP | Java

The call() method sets the this value of join() to the languages object.

This technique can be helpful when working with older browser APIs or custom objects that behave like arrays.

The method also handles a zero-length array-like object by returning an empty string.

const data = {
    length: 0
};

const result = Array.prototype.join.call(data, ", ");

console.log(result);

Output:

How JavaScript join() Converts Different Data Types

The JavaScript join() method converts array elements into strings before combining them. This means an array can contain numbers, strings, booleans, objects, and other values.

However, different data types may produce different string representations.

const values = [
    "JavaScript",
    100,
    true,
    false,
    null,
    undefined
];

const result = values.join(" | ");

console.log(result);

Output:

JavaScript | 100 | true | false |  | 

JavaScript converts the number 100 into "100", true into "true", and false into "false". The null and undefined values contribute empty strings.

This behavior is useful when combining mixed data into a single text value. However, remember that the resulting string does not preserve the original data types.

For example:

const values = [10, 20, 30];

const result = values.join(", ");

console.log(typeof result);
console.log(result);

Output:

string
10, 20, 30

The original values are numbers, but the returned value is a string.

Using join() with Conditional Data

Sometimes an application contains optional values. You may want to combine only the values that are available.

The filter() and join() methods can work together for this purpose.

const user = {
    name: "Rahim",
    city: "Dhaka",
    country: "",
    profession: null
};

const details = [
    user.name,
    user.city,
    user.country,
    user.profession
];

const result = details
    .filter(Boolean)
    .join(", ");

console.log(result);

Output:

Rahim, Dhaka

The filter(Boolean) expression removes falsy values such as empty strings, null, undefined, false, and 0.

Be careful with this approach: it also removes valid values such as the number 0 and the boolean false. If those values should remain, use an explicit condition instead.

const values = ["Rahim", "", null, 0, undefined, "Dhaka"];

const result = values
    .filter(value => value !== null && value !== undefined && value !== "")
    .join(", ");

console.log(result);

Output:

Rahim, 0, Dhaka

This version removes only the values you specifically want to exclude.