freeCodeCamp Challenge Guide: Use Bracket Notation to Find the Last Character in a String

Use Bracket Notation to Find the Last Character in a String


Hints

Hint 1

Consider the following string:

var str = "Coding";

This string has a length of 6 characters, so if you use .length on the string, it will give you 6. But remember that the first character is at the zero-th position. The second character is at the first position. The third character is at the second position.

Keep on going, and you eventually get that the sixth character (which, based on the above string, is β€˜g’) is at the fifth position. That’s why you obtain the last character of a string, with:

var lastChar = str[str.length - 1]; // This is 6 - 1, which is 5

This will be β€˜g’.

4 Likes

var lastLetterOfLastName = lastName[lastName.length - 1];

4 Likes