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']This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys6Sets Part 2
Math - Union Of SetsMath - Intersection Of SetsMath - Difference Of SetsMath - Symmetric DifferenceSubsets And SuperSetsRecap - Group Friends9Advanced Array methods
The Join & Split MethodsThe Map Method The Filter MethodThe Reduce MethodChaining Array MethodsForEach And Map Are Same?The Sort MethodThe Find & FindIndex MethodRecap - The Chain Master