Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Splitting on the First Colon

Splitting on the First Colon

In this lesson, you ported the helper’s first line, which takes PAY-2291’s header apart at its first colon. The header has two colons, one after the key and one inside the title. In Python, key, title = header.split(":", 1) gives the key and everything after the first colon, and the helper prints Commit: PAY-2291: Refund emails show the wrong currency: EUR shown as USD.

  • The natural port, const [key, rawTitle] = header.split(":", 1);, unpacks an array, JavaScript’s version of a list (arrays get a course of their own later), with destructuring, the same shape as swapping with [a, b] = [b, a]. It stops on the next line with TypeError: Cannot read properties of undefined (reading 'trim').
  • The second argument means different things. Python’s is maxsplit: it counts splits and keeps the rest of the text in the last piece. JavaScript’s counts pieces, so header.split(":", 1) is [ 'PAY-2291' ] and rawTitle gets undefined.
  • On this header, Python’s split gives two pieces. A header with no colon gives it one, as in "PAY-2291 Refund emails".split(":", 1), and then Python’s unpacking stops with a ValueError. JavaScript’s destructuring fills the missing name with undefined instead, and the error comes a line later.
  • The repair split(":", 2) runs without an error and prints Commit: PAY-2291: Refund emails show the wrong currency. With no second argument, split(":") works as it does in Python, cutting at every colon into three pieces; the limit caps how many of those pieces you keep, so 2 keeps the first two and silently drops : EUR shown as USD.
  • To split at the first separator only, find it with indexOf() and slice either side. header.indexOf(":") is 8, as Python’s find() gives. Python’s partition() does the same job in one call, and JavaScript has no equivalent.
  • If there’s no colon, indexOf() returns -1, as find() does, and slicing with it quietly gives the wrong pieces in either language, so check for -1 first when the input might not have a colon. Splitting on patterns is a later topic.

Here’s the port with the fix in place:

Language: JavaScript
const header =
  "PAY-2291: Refund emails show the wrong currency: " +
  "EUR shown as USD";

const colon = header.indexOf(":");
const key = header.slice(0, colon);
const title = header.slice(colon + 1).trim();
console.log(`Commit: ${key}: ${title}`);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 In this lesson, you’ll port the helper’s first line. It takes PAY-2291’s header apart at its first colon. At the start of the course, that line gave two different answers.

00:15 Python gave the key and the whole title, and JavaScript gave the key alone.

00:20 In the Python helper, the header has two colons: one after the key, and one inside the title. split(":", 1) splits at the first colon only, so key gets PAY-2291, and title gets the rest.

00:37 strip() trims the space at the front. Python prints the commit subject with the whole title, second colon included.

00:46 The header carries over with const in front. It’s two strings, joined with + across two lines. The split line keeps Python’s call word for word.

00:58 Python’s unpacking becomes destructuring. Square brackets unpack an array into two names. An array is JavaScript’s version of a list. Arrays get a course of their own later.

01:11 Destructuring has the same shape as swapping two variables. A const can’t be reassigned, so the raw piece goes in rawTitle, and title holds the trimmed one.

01:22 strip() becomes trim() on the title line. The commit line turns into a template literal.

01:28 Run the port, and JavaScript stops with a TypeError on the trim line. It can’t read properties of undefined, so rawTitle holds no string at all.

01:41 The second argument is where the two languages differ. In Python, the 1 is maxsplit, and it counts splits. One cut: the rest stays in the last piece.

01:53 In JavaScript, the 1 counts pieces instead, so the array holds one piece: the key alone. There’s nothing left for rawTitle to hold.

02:04 On this header, Python’s split gives two pieces. A header with no colon would give it one, and then Python’s unpacking would stop there, with a ValueError for too few values.

02:17 JavaScript fills the missing name with undefined instead. The error only shows up a line later, where the title is trimmed.

02:27 The 1 counts pieces. The destructuring has two names to fill. So the repair makes the limit a 2 instead. Python’s subject has the whole title.

02:39 What will the commit line print now? Your turn: pause the video and predict.

02:49 JavaScript answers without an error, and the title stops at the word currency.

02:55 The second colon and the rest are gone. With no limit, split(":") cuts at every colon, as in Python. That gives three pieces. The limit caps how many pieces you keep.

03:10 The 2 kept two and dropped the third. That’s where the likeness stops. Python’s maxsplit keeps the rest in the last piece, and JavaScript’s limit throws it away, silently.

03:24 For the first colon only, a JavaScript developer finds the colon first. The indexOf() method finds it at position 8, as Python’s find() does.

03:35 slice() takes the text before the colon, and that’s the key. A second slice(), starting one past the colon, keeps everything after it, second colon included.

03:47 trim() drops the space at the front. Run the port, and JavaScript prints the same subject as Python, colon and all. With no colon, though, indexOf() gives -1, as find() does.

04:01 These slices go quietly wrong in either language, so check first. Python’s partition() would do this in one call. JavaScript strings have no partition(), so indexOf() and slice() are how you write it.

04:17 To cut at every separator, reach for split(), as in Python. Its second argument caps the pieces you keep, and it drops the rest. For the first separator only, find it with indexOf(), then slice either side.

04:33 Splitting on patterns is a later topic.

04:37 Next up is Joining Words Into a Branch Name, a text lesson. There, you’ll split the title into its words, keep the first six, and join them with hyphens.

Become a Member to join the conversation.