join method
The join
method joins the array
elements into a string with the specified
separator (it will be inserted between
the array elements). The separator
(or delimiter) is specified by the
method parameter and is optional. If
it is not specified, a comma will be
used as a separator by default. If you
want to merge array elements without
a separator - specify it as an empty
string ''
.
Syntax
array.join([separator]);
Example
Let some array be given. Let's concatenate
the elements of this array into a string
with the separator '-'
:
let arr = [1, 2, 3];
let str = arr.join('-');
console.log(str);
The code execution result:
'1-2-3'
Example
And now let's not specify a separator and the default separator will be a comma:
let arr = [1, 2, 3];
let str = arr.join();
console.log(str);
The code execution result:
'1,2,3'
Example
Let's merge array elements without any separator:
let arr = [1, 2, 3];
let str = arr.join('');
console.log(str);
The code execution result:
'123'
Example . Usage
Let's reverse the characters of a string. To
do this, split the string into an array using
split
using the separator ''
(this separator
will put each character of the string into
individual array element), reverse this array
with reverse
and then join the reversed array back with
join
:
let str = '123456789';
let arr1 = str.split('');
let arr2 = arr1.reverse();
let res = arr2.join('');
console.log(res);
The code execution result:
'987654321'
Example . Usage
Let's simplify the solution of the previous problem - let's merge all the commands into a chain:
let str = '123456789';
let res = str.split('').reverse().join('');
console.log(res);
The code execution result:
'987654321'
Example . Usage
The date is given in the format
'2025-12-31'
. Let's make it
into the format '31.12.2025'
. To
do this, split the string into an array
using split
,
reverse this array using
reverse
and then merge the reversed array back
with join
:
let date = '2025-12-31';
let res = date.split('-').reverse().join('.');
console.log(res);
The code execution result:
'31.12.2025'