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
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.indexOf(), includes(), lastIndexOf()
These methods search an array for a specific value. indexOf() returns the first matching index (or -1 if not found), lastIndexOf() searches from the end, and includes() simply returns true or false. All use strict equality (===) for comparison, so they cannot find objects by content, only by exact reference.
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.indexOf(), includes(), lastIndexOf()
These methods search an array for a specific value. indexOf() returns the first matching index (or -1 if not found), lastIndexOf() searches from the end, and includes() simply returns true or false. All use strict equality (===) for comparison, so they cannot find objects by content, only by exact reference.