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

Read Characters by Position

In this lesson, you read single characters out of a string to make a corner tag for the pizza party’s place cards: the guest’s first letter and last letter, Ge for Grace.

Key Points

  • Every character in a string has a position, and JavaScript counts positions from 0. A position is the number of characters that come before it, so in "Grace", G is at 0 and c is at 3, even though it’s the fourth letter.
  • To read a character, write the name and then the position in square brackets: guest[0] gives "G".
  • Every name has its own length, so don’t hard-code the last position. As long as the string isn’t empty, the last position is one less than its length: guest[guest.length - 1]. JavaScript works out guest.length - 1 first, and then reads that position.
  • A position the string doesn’t have gives undefined, with no error. guest[5] is undefined, and an empty string "" gives undefined for every position. A wrong position doesn’t stop your script, so check what it prints.
  • at() also reads one position, and it takes a negative position that counts back from the end: guest.at(-1) gives the last character, the same as guest[guest.length - 1]. Only at() counts back. In square brackets, -1 isn’t a position the string has, so guest[-1] gives undefined.
  • Reading a position copies a character out and leaves the string as it was. To get different text, you build a new string, like joining two characters with +.

The Final Code

Language: JavaScript Filename: cards.js
const guest = "Grace";
const tag = guest[0] + guest.at(-1);
console.log(tag);

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:00 So far, you’ve joined strings together. Now you’ll go the other way and read single characters out of one. The pizza party needs place cards. Each card gets a small tag in the corner, made of the guest’s first letter and last letter.

00:14 So for Grace, the tag is a capital G and a small e. Your script starts with the constant guest, which points at the string Grace, and the tag needs two of its characters.

00:29 Every character in a string has a position, and JavaScript counts positions from zero. A position is the number of characters that come before it. Nothing comes before G, so G is at 0.

00:42 One character comes before r, so r is at 1. And so on, up to e at 4. To read a character, write the name and then the position in square brackets. Log that, run the script, and it prints a capital G.

00:58 Now change the 0 to a 3. Your turn: pause the video and predict which character position 3 gives.

01:11 It’s a small c. Three characters come before c, so c is at position 3, even though it’s the fourth letter.

01:22 The tag also needs the last letter. For Grace, you could count and write 4. But every name has its own length, so a fixed number won’t fit every card. The string already knows its length.

01:35 Grace has five characters, so its length is 5. Positions start at 0, so as long as the string isn’t empty, the last position is one less than the length. So inside the brackets, you write guest.length - 1.

01:51 JavaScript works that out to 4 first, and then it reads position 4. Run the script, and you get a small e.

02:01 What if you ask for a position the string doesn’t have? Grace’s positions stop at 4, and this line asks for position 5. Your turn: pause the video and predict what this line prints.

02:18 It prints undefined, with no error.

02:22 There’s no character at position 5, so JavaScript gives back undefined, its way of saying there’s no value here. You’ll see more of it later. An empty string has no positions at all, so it gives undefined for every one.

02:36 A wrong position doesn’t stop your script, so check what it prints.

02:42 There’s a shorter way to reach the end. Strings come with a tool called at(), spelled a-t, that reads one position. You write a dot after the name, then at, and the position in parentheses.

02:55 That’s all you need about tools like this for now. The at() tool also takes a negative position, which counts back from the end. -1 is the last character.

03:06 So guest.at(-1) gives you a small e, the same as length minus 1. Only the at() tool counts back. In square brackets, -1 isn’t a position the string has, so it gives undefined instead of a letter.

03:24 Now you have both pieces. Position 0 gives the first letter, and at(-1) gives the last. Join them with a plus into a new string, tag, for the card.

03:34 Log tag and run the script. It prints a capital G and a small e. Notice that guest still points at Grace. Reading a position copies a character out and leaves the string as it was.

03:48 When you need different text, you build a new string, like this tag.

03:53 So here’s the rule. Positions count from zero, the last position is the length minus 1, and at(-1) reads that last one too. A position past the end gives undefined instead of a character.

04:08 Next up is Make Initials From Names, an exercise where you’ll read characters out of guests’ names to build their initials. Have fun with those initials.

Become a Member to join the conversation.