Menu
Coddy logo textTech

The Join & Split Methods

Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 51 of 65.

The join() method creates and returns a new string by concatenating all elements in an array, separated by a specified separator.

The split() method divides a string into an ordered list of substrings and returns them in an array.

Basic syntax:

array.join([separator])
string.split([separator])

For example:

<strong>Join()</strong>:

const fruits = ['apple', 'banana', 'orange'];
const str = fruits.join(', ');
console.log(str); // "apple, banana, orange"

<strong>Split()</strong>:

const sentence = "Hello World";
const words = sentence.split(' ');
console.log(words); // ['Hello', 'World']

You can use different separators:

const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.join('')); // “applebananaorange”
console.log(fruits.join('-')); // "apple-banana-orange"
const date = "2023-05-15";
console.log(date.split('-')); // ['2023', '05', '15']
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

Cheat sheet

The join() method creates a string by concatenating array elements with a separator:

array.join([separator])

The split() method divides a string into an array of substrings:

string.split([separator])

Examples:

const fruits = ['apple', 'banana', 'orange'];
const str = fruits.join(', '); // "apple, banana, orange"

const sentence = "Hello World";
const words = sentence.split(' '); // ['Hello', 'World']

// Different separators
fruits.join(''); // "applebananaorange"
fruits.join('-'); // "apple-banana-orange"
"2023-05-15".split('-'); // ['2023', '05', '15']

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow