Table of contents
1.
Introduction
2.
Description  
3.
Syntax
4.
Parameters
5.
Return Value  
6.
Examples
6.1.
Example 1: Basic Return Value  
6.2.
Example 2: Using join() with the Default Separator
6.3.
Example 3: Using join() with a Custom Separator
6.4.
Example 4: Using join() with an Empty Separator
6.5.
Example 5: Empty Array  
6.6.
Example 6: Using join() with a Line Break
6.7.
Example 7: Using join() on an Empty Array
6.8.
Example 8: Custom Separator  
7.
Practical Use Case  
8.
Supported Browsers
9.
Frequently Asked Questions
9.1.
What happens if I don’t provide a separator in join()? 
9.2.
Can join() be used on non-array objects? 
9.3.
How can I use join() to create a sentence from an array of words? 
10.
Conclusion
Last Updated: Feb 12, 2025
Medium

JavaScript Array join() Method

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The JavaScript Array join() method is a built-in function that allows developers to join all elements of an array into a single string. This method takes an optional separator, which can be a string used to separate each array element in the resulting string. By default, if no separator is provided, the elements are joined with a comma. This method is commonly used when you need to create a string representation of an array, such as when displaying data or formatting information. It is an efficient and easy-to-use tool in JavaScript for handling array data.

JavaScript Array join() Method

In this article, you will learn about the syntax of the array join() method, how it works, and different use cases to implement it effectively in your JavaScript projects.

Description  

The `join()` method in JavaScript is a built-in function that converts all elements of an array into a single string. It takes one optional parameter, which is the separator. This separator determines how the elements will be joined together in the final string. If you don’t provide a separator, the elements will be joined with a comma by default. The original array remains unchanged because `join()` does not modify it; instead, it returns a new string.

 

Imagine you have an array like this:  

let fruits = ["apple", "banana", "cherry"];


If you use the `join()` method without any separator, here’s what happens:  

let result = fruits.join();
console.log(result); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"apple,banana,cherry"


In this case, the elements are joined with a comma because no separator was specified. Now, let’s say you want to join the elements with a space or a hyphen. You can pass the desired separator as an argument to the `join()` method. For example:  

let resultWithSpace = fruits.join(" ");
console.log(resultWithSpace); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"apple banana cherry"

 

let resultWithHyphen = fruits.join("-");
console.log(resultWithHyphen); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"apple-banana-cherry"


You can even use special characters or longer strings as separators. For example:  

let customSeparator = fruits.join(" | ");
console.log(customSeparator); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

"apple | banana | cherry"


This flexibility makes the `join()` method useful in many scenarios, such as creating CSV files, formatting data for display, or generating dynamic content for web pages.  

Syntax

array.join(separator)

Parameters

The join() method accepts one optional parameter:

  1. separator (optional): This defines the character or string that separates the elements in the output string. If omitted, the default separator is a comma (,).

Return Value  

The `join()` method in JavaScript always returns a string. This string is created by concatenating all the elements of the array, separated by the specified separator. If the array is empty, the method will return an empty string. Understanding the return value is important because it helps you decide how to use the output in your program.

Let’s look at some examples to understand this better.  

Examples

Let’s explore different use cases of the join() method.

Example 1: Basic Return Value  

Let’s take a simple example where we join an array of numbers:  

let numbers = [1, 2, 3, 4];
let result = numbers.join();
console.log(result); 
console.log(typeof result); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

"1,2,3,4"
"string"


In this case, the `join()` method converts the array into a string, and the default separator (a comma) is used. The `typeof` operator confirms that the return value is indeed a string.  

Example 2: Using join() with the Default Separator

let fruits = ["Apple", "Banana", "Mango"];
let result = fruits.join();
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Apple,Banana,Mango

 

Since no separator is provided, JavaScript uses the default comma (",").

Example 3: Using join() with a Custom Separator

let fruits = ["Apple", "Banana", "Mango"];
let result = fruits.join(" - ");
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Apple - Banana - Mango


Here, we have specified " - " as the separator.

Example 4: Using join() with an Empty Separator

let numbers = [1, 2, 3, 4, 5];
let result = numbers.join("");
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

12345


When an empty string ("") is used as a separator, all elements are joined without any space or symbol.

Example 5: Empty Array  

If the array is empty, the `join()` method will return an empty string. Let’s see how it works:  

let emptyArray = [];
let result = emptyArray.join();
console.log(result); // Output: ""
console.log(typeof result); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"string"


Even though there are no elements in the array, the return value is still a string, but it’s just empty.  

Example 6: Using join() with a Line Break

let lines = ["Hello", "How are you?", "Goodbye"];
let result = lines.join("\n");
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Hello
How are you?
Goodbye

 

Using "\n" as the separator ensures each element appears on a new line.

Example 7: Using join() on an Empty Array

let emptyArray = [];
let result = emptyArray.join(" - ");
console.log(result);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

When join() is used on an empty array, it returns an empty string.

Example 8: Custom Separator  

When you specify a custom separator, the return value reflects that separator. For instance:  

let colors = ["red", "green", "blue"];
let result = colors.join(" + ");
console.log(result); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"red + green + blue"


Here, the separator `" + "` is used to join the elements, and the return value is a string with the specified format.  

Practical Use Case  

The return value of the `join()` method can be used directly in other parts of your code. For example, if you’re building a web application and need to display a list of items, you can use `join()` to format the data before showing it to the user.  

let items = ["laptop", "phone", "tablet"];
let formattedList = "You have selected: " + items.join(", ");
console.log(formattedList); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"You have selected: laptop, phone, tablet"


In this example, the return value from `join()` is concatenated with another string to create a meaningful message for the user.  

Supported Browsers

The join() method is widely supported in all modern browsers:

  • Google Chrome 
     
  • Mozilla Firefox 
     
  • Microsoft Edge 
     
  • Safari
     
  • Opera 
     
  • Internet Explorer  (limited support in very old versions)

Frequently Asked Questions

What happens if I don’t provide a separator in join()? 

If no separator is provided, the default separator (,) is used.

Can join() be used on non-array objects? 

No, join() is specifically for arrays. If you try it on other objects, it will result in an error.

How can I use join() to create a sentence from an array of words? 

You can use " " (space) as a separator.

Conclusion

In this article, we learned about the JavaScript Array join() method and how it helps to combine array elements into a single string. We covered how to use different separators and handle special cases. By understanding this method, developers can easily work with arrays in JavaScript, making their code cleaner and more efficient.

Live masterclass