Syntax
arr.concat(otherArr)
arr.join(separator)Examples
concat() - Merging Arrays
Combining arrays without mutating the originals.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const merged = arr1.concat(arr2);
console.log(merged); // [1, 2, 3, 4, 5, 6]
console.log(arr1); // [1, 2, 3] - unchanged
// concat() can also merge multiple arrays and add individual values
const combined = arr1.concat(arr2, [7, 8], 9);
console.log(combined); // [1, 2, 3, 4, 5, 6, 7, 8, 9]join() - Array to String
Converting array elements into a formatted string.
const words = ["Hello", "World", "!"];
console.log(words.join(" ")); // "Hello World !"
console.log(words.join()); // "Hello,World,!" - default separator is a comma
console.log(words.join("")); // "HelloWorld!" - no separator
const path = ["usr", "local", "bin"];
console.log(path.join("/")); // "usr/local/bin"Best practices
- Prefer the spread operator ([...arr1, ...arr2]) over concat() in modern code - it reads more clearly, though both achieve the same result
- Use join() instead of manually looping and building a string when converting an array to display text
- Choose a join() separator that matches your output format - commas for CSV-like data, spaces for sentences, "/" for paths
- Remember join() converts non-string elements using their default string representation, which may need formatting first for numbers or objects
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification