Problem Statement
What does the split() method return?
Explanation
The split method divides a string into an array of substrings based on a separator.
The separator can be a string or regular expression. It is removed from the result.
If separator is empty string, split returns array of individual characters.
You can limit the number of splits with a second parameter.
The join method does the opposite, combining array elements into a string.
Split and join are commonly used together for string manipulation.
Code Solution
SolutionRead Only
const str = 'Hello World JavaScript';
// Split by space
const words = str.split(' ');
console.log(words); // ['Hello', 'World', 'JavaScript']
// Split into characters
const chars = 'Hello'.split('');
console.log(chars); // ['H', 'e', 'l', 'l', 'o']
// Split with limit
const limited = str.split(' ', 2);
console.log(limited); // ['Hello', 'World']
// Split CSV
const csv = 'John,30,Developer';
const data = csv.split(',');
console.log(data); // ['John', '30', 'Developer']
// Join - opposite of split
const arr = ['Hello', 'World'];
const joined = arr.join(' ');
console.log(joined); // 'Hello World'
// Join with different separator
const path = ['folder', 'subfolder', 'file.txt'];
console.log(path.join('/')); // 'folder/subfolder/file.txt'
console.log(path.join('\\'));// 'folder\\subfolder\\file.txt'
// No separator in join (uses comma)
console.log(arr.join()); // 'Hello,World'
// Reverse string
const original = 'Hello';
const reversed = original.split('').reverse().join('');
console.log(reversed); // 'olleH'
// Count words
const sentence = 'This is a test';
const wordCount = sentence.split(' ').length;
console.log(wordCount); // 4