Table of Contents
What join() Does
- Combines array items into a string: every item is converted to text and placed one after another.
- Uses a separator you choose: the separator goes between the items, not at the start or the end.
- Returns a new string: the result is a piece of text, not an array.
- Leaves the original array alone: nothing in the array is changed.
let fruits = ["apple", "banana", "mango"];
let text = fruits.join(", ");
console.log(text); // "apple, banana, mango"
console.log(fruits); // ["apple", "banana", "mango"], unchanged
Syntax and Parameters
array.join(separator);
separator: the text placed between the items. This is optional.
If you leave it out: the items are separated by a comma, with no space.
An empty string: the items are joined with nothing in between.
let letters = ["a", "b", "c"];
console.log(letters.join()); // "a,b,c"
console.log(letters.join(" ")); // "a b c"
console.log(letters.join("-")); // "a-b-c"
console.log(letters.join("")); // "abc"
Practical Examples
Building a readable list:
let skills = ["HTML", "CSS", "JavaScript"];
console.log("Skills: " + skills.join(", ")); // "Skills: HTML, CSS, JavaScript"
Creating a URL-friendly name:
let words = ["my", "first", "blog", "post"];
let slug = words.join("-");
console.log(slug); // "my-first-blog-post"
Building a file path:
let parts = ["home", "user", "documents"];
console.log(parts.join("/")); // "home/user/documents"
Putting items on separate lines:
let lines = ["Line one", "Line two", "Line three"];
console.log(lines.join("\n"));
// Line one
// Line two
// Line three
Joining the results of map():
let users = [
{ name: "Vi", age: 22 },
{ name: "Sam", age: 30 }
];
let names = users.map(user => user.name).join(", ");
console.log(names); // "Vi, Sam"
let names = users.map(user => user.name).join(", "); console.log(names); // "Vi, Sam"The Opposite of split()
join() turns an array into a string, while split() turns a string into an array.
let text = "red,green,blue";
let colors = text.split(",");
console.log(colors); // ["red", "green", "blue"]
let backToText = colors.join(",");
console.log(backToText); // "red,green,blue"
Together, they make quick text changes easy:
let sentence = "hello world from javascript";
let result = sentence.split(" ").join("-");
console.log(result); // "hello-world-from-javascript"
A common example: reversing a string:
let word = "hello";
console.log(word.split("").reverse().join("")); // "olleh"
Things to Remember
Every item is converted to text: numbers, booleans, and other values become strings.
console.log([1, 2, 3].join("+")); // "1+2+3"
null and undefined become empty text: they do not show up as the words "null" or "undefined".
console.log([1, null, 3, undefined, 5].join("-")); // "1--3--5"
Objects become "[object Object]": which is rarely what you want.
let items = [{ name: "Vi" }, { name: "Sam" }];
console.log(items.join(", ")); // "[object Object], [object Object]"
Use map() first for objects: pull out the property you need, then join.
console.log(items.map(item => item.name).join(", ")); // "Vi, Sam"
Nested arrays are flattened into text: their items are joined with commas.
console.log([1, [2, 3], 4].join("-")); // "1-2,3-4"
An empty array gives an empty string: with no error.
console.log([].join(", ")); // ""
Common Mistakes
- Expecting an array back: join() returns a string, so array methods such as map() will not work on the result.
- Forgetting to choose a separator: without one, items are separated by a comma and no space, which can look untidy.
- Joining objects directly: you will get "[object Object]", so use map() first.
- Expecting the original array to change: join() returns a new string, so save the result in a variable.
Conclusion
Frequently Asked Questions (FAQs)
It returns a string made from all the items in the array.
2. What happens if I do not pass a separator?
3. What is the difference between join() and split()?The items are separated by a comma, with no space.
join() turns an array into a string, while split() turns a string into an array.
0 Comments