Most of the text in a web application arrives as one long string, and it has to become structure. A comma-separated list, a query string, a log line, and a number that somebody typed into a form all start that way. Splitting and joining are the two halves of that job, and the conversion from text to a number is where most of the bugs hide. The earlier lessons in this module gave you the string methods and the template literals, and this lesson uses both of them, because slice() is how you pull one piece out of a line and a template literal is how you put a message back together. The lesson after this one takes the pieces you get from splitting and goes looking for things inside them.
The previous lesson in this module covered "Template Literals in Real Code", and the next lesson in this module is about "Searching and Replacing in Strings".
What does split actually return?
split(separator) cuts a string into an array of pieces. The separator is a string or a regular expression, and the result always contains at least one element.
const line = 'Ada,Grace,Linus';
console.log(line.split(',')); // [ 'Ada', 'Grace', 'Linus' ]
console.log('a,b,c'.split(',', 2)); // [ 'a', 'b' ]
console.log('a,,b'.split(',')); // [ 'a', '', 'b' ]
console.log('solo'.split(',')); // [ 'solo' ]
Four behaviours are worth remembering. The second argument is a limit, and the array is cut short there while the rest of the string is thrown away rather than kept, so 'a,b,c'.split(',', 2) loses the c completely. Empty fields are kept, which is why 'a,,b' gives you an empty string in the middle instead of skipping it. A string that holds no separator produces a one-element array and never an empty one. And a separator that is more than one character works in exactly the same way, so 'a::b'.split('::') gives you two pieces.
How do I join a list back into text?
join(separator) is the opposite operation. It turns an array of pieces back into one string, and it puts the separator between each pair of pieces.
const parts = ['shoes', 'socks', 'hat'];
console.log(parts.join(', ')); // shoes, socks, hat
console.log(parts.join(' and ')); // shoes and socks and hat
console.log([1, 2, 3].join('-')); // 1-2-3
console.log(['a', 'b'].join()); // a,b
Called with no argument, join uses a comma. The detail that catches people is how it treats values that are not strings. null and undefined become empty strings instead of the literal text, which is convenient when you are building a list out of optional values and can also hide a bug, so make sure that you meant it.
How do I read a query string?
For URLs, the platform already ships a parser. URLSearchParams handles the encoding and the decoding for you.
const query = 'name=Ada&role=admin&page=2';
const params = Object.fromEntries(new URLSearchParams(query));
console.log(params.name); // Ada
console.log(params.page); // 2
There are two things to notice. The values come back as strings, so page is the text '2' and you need to convert it before you do any arithmetic with it. And the encoding is handled automatically, so a space that was written as %20 arrives as a real space. If the same key appears twice, URLSearchParams keeps both values, and you can read all of them with getAll.
How do I turn a string into a number?
Converting text into a number has two traps, and both of them give you a number that you did not expect.
const input = ' 42 ';
console.log(Number(input.trim())); // 42
console.log(parseInt(input, 10)); // 42
console.log(Number('42px')); // NaN
console.log(parseInt('42px', 10)); // 42
Number() is strict. It accepts the entire string or it gives you NaN, which is why '42px' becomes NaN. parseInt is lenient, because it reads the leading digits and ignores everything after them, so '42px' quietly becomes 42. That is exactly why you should always pass the radix, and why you should prefer Number() for anything that has to be entirely numeric. The whitespace case is the other trap. Number(' 42 ') is 42 because the conversion ignores the whitespace around it, but Number('') and Number(' ') are both 0, which is not what anybody expects from an empty field.
How do I parse a fixed-format line?
Log files and fixed-width records are the classic case for splitting, because you know the shape of the data before you start.
const log = '2026-03-04 09:15:22 ERROR payment timeout';
const parts = log.split(' ');
console.log(parts[0], parts[1], parts[2]);
// 2026-03-04 09:15:22 ERROR
console.log(parts.slice(3).join(' '));
// payment timeout
Split on a single space, then take the tail with slice and join. The fixed fields come from known positions, and everything after the last fixed field is free text that you rejoin. Splitting on a plain space rather than on a regular expression keeps the empty fields, which matters when the message happens to contain a double space.
Which text parsing helpers should I know?
| Helper | What it does |
|---|---|
str.split(sep) | Turns a string into an array of pieces |
arr.join(sep) | Turns an array back into one string |
str.trim() | Removes the whitespace at both ends |
str.replaceAll(a, b) | Replaces every occurrence |
Number(str) | Strict numeric conversion, or NaN |
parseInt(str, 10) | Lenient numeric conversion |
new URLSearchParams(q) | Parses and builds query strings |
new Intl.NumberFormat(...) | Formats numbers for people to read |
Try it yourself: write a file called parse.js and start with const csv = 'pen,3;book,12;lamp,20';. Split it on ';' and then on ',' to produce three pairs, and print the pairs. Build an array of product names and an array of prices, and then print the total price. Next, take a query string such as 'page=2&sort=name', read page and sort with URLSearchParams, and convert page with Number before printing it. Finally, write a small function toCsv(rows) that takes an array of arrays and joins each row with commas, and run it over your three pairs.