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

Reading Characters With [] and at()

Reading Characters With [] and at()

In this lesson, you ported the helper’s full-stop check and read single characters the JavaScript way. The review bot rejects a commit subject that ends with a full stop, and PAY-2304’s title, Retry failed card payments., has one.

  • Python’s len() function becomes the length property in JavaScript, read after a dot with no parentheses: title.length is 27. Watch out: len() doesn’t exist in JavaScript, and title.length() is a TypeError, because a property isn’t a function.
  • Brackets read a position counted from the start, as in Python: title[0] is R, and title[26], one less than the length, is the full stop.
  • JavaScript’s brackets don’t count from the end: they look up a position by its number, and the string has no position -1. The natural port of the check, const last = title[-1];, gives undefined, JavaScript’s value for nothing there, so the check prints Ends with a full stop: false where Python prints True, with no error.
  • Reading a position that isn’t there gives undefined in JavaScript, with no error. A position past the end gives undefined too, where Python raises an IndexError, so check the length when the text might be short.
  • To count from the end, use the at() method: a negative position counts back from the end, as in a Python index, so title.at(-1) is the full stop. Like the brackets, at() gives undefined, not an error, when the position isn’t there.
  • at() takes positions from the start too, but JavaScript code keeps the brackets for those and reaches for at() when it counts from the end.
  • As in Python, a string can’t change in place. In the helper’s port, title[0] = "r" stops with a TypeError.
  • For text like these titles, each position holds one character. Text with emoji is where JavaScript’s positions and Python’s part ways, a later topic.

Here’s the check with the fix in place:

Language: JavaScript
const title = "Retry failed card payments.";

const last = title.at(-1);
console.log(`Ends with a full stop: ${last === "."}`);

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 one check from the branch-name helper and read single characters the JavaScript way. The review bot rejects any commit subject that ends with a full stop.

00:15 So the helper checks the last character of the ticket’s title. In Python, title[-1] reads it, counting back from the end. PAY-2304’s title ends with a full stop, so the script’s answer is True for this ticket.

00:33 The title line carries over with const in front. Before the last character, take two reads you already know from Python. Python’s len() is a function you call on the text.

00:45 JavaScript keeps the size on the string itself, as its length property, read after a dot with no parentheses. title.length counts the same 27 characters.

00:59 Watch out for the Python habit here: len() doesn’t exist in JavaScript, and length() with parentheses is a TypeError, because a property isn’t a function.

01:10 Brackets read a position counted from the start, as in Python, so position 0 is the capital R.

01:18 With the brackets working as in Python, the check’s title[-1] ports as it is. The print line becomes console.log() with a template literal, with $ in front of the hole as in the commit message you wrote, and === takes the place of == in the comparison.

01:36 Python said yes, this title ends with a full stop. What will JavaScript print on the last line? Your turn: pause the video and predict.

01:51 JavaScript answers false, with no error and no warning. The check misses the full stop that’s right there in the title. Python’s negative positions count back from the end, so -1 is the full stop.

02:09 JavaScript’s brackets only start from the front: 0 is the R, and 26, one less than the length, is the full stop. Brackets look up a position by its number, and -1 isn’t one this string has.

02:24 So title[-1] gives undefined, JavaScript’s value for nothing there.

02:31 Reading a position that isn’t there doesn’t stop JavaScript with an error. The undefined travels on, into a comparison with a full stop that comes out false without a word.

02:42 The same goes for a position past the end, where Python raises an IndexError, so check the length when the text might be short.

02:53 To count from the end, JavaScript has the at() method. The fix swaps the brackets for a call to it. Like the brackets, at() takes a position, but a negative one counts back from the end, as Python’s brackets do, so -1 is the full stop.

03:09 And like the brackets, it gives undefined, not an error, when the position isn’t there. Run the port again, and the last line prints true, matching Python apart from JavaScript’s lowercase spelling.

03:25 at() reads positions from the start too. But JavaScript code keeps the brackets for those, and reaches for at() when it counts from the end.

03:36 Brackets count from the start, at() counts from the end, and the length is a property. As in Python, a string can’t change in place: in the helper’s port, assigning a new first letter stops with a TypeError.

03:50 For text like these titles, each position holds one character; text with emoji is where JavaScript’s positions and Python’s part ways, a later topic.

04:01 Next up is Slicing Text With slice(), a text lesson. There, you’ll cut the commit subject to fifty characters and read the ticket number with the slice() method, where your Python intuition carries over.

Become a Member to join the conversation.