Logic question regarding Use Bracket Notation to Find the Last Character in a String

So, I know the answer is var lastLetterOfLastName = lastName[lastName.length - 1]; but my question is, why doesn’t var lastLetterOfLastName = lastName.length[- 1]; work?

Because you are using the .length so it will be whatever the length it is and the bracket notation is trying to access a number using -1. so lastName.length will return a number. then you’re trying to use the bracket notation to access -1 of that returned number which will spit out an error. Hope this helps…

1 Like

lastName.length isn’t an array and even if it was -1 is not a valid array index.


OH! Duh, I was totally remembering the indexing commands incorrectly. I was thinking to the command to find a non-negative index was variable.length[0].

Apparently, I was up too late last night studying. :slight_smile:

Brain farts happen :brain: :dash:

1 Like