Slicing Text With slice()

With .at(-1), the port of ticket_branch.py reads the last character of a title. The commit subject needs a range, though. The review bot accepts at most 50 characters, and Python truncates the subject with [:50]. JavaScript’s brackets hold one position only, so a slice needs a new spelling.

Cut the Subject With slice()

JavaScript has no [a:b] syntax. The .slice() method does the same job, with the start and the end as arguments. Here’s the helper’s f"{key}: {title}"[:50] ported for PAY-2291:

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

const subject = `${key}: ${title}`.slice(0, 50);
console.log(`Commit: ${subject}`);

The cut falls inside EUR, exactly where Python’s does:

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

Become a Member to join the conversation.