Im wonder why is code using length property in order to find any character from the last one? Im talking about challenge down bellow:
The challenge is specifically asking about the nth character from the end. If a string is 4 characters long, then the second to last character will be at index 2. If the string is 178 characters long, the second to last character will be at index
176. We don’t want to hard code the length of the string in, so we let the computer do the counting for us. The last character of a string
someString will always be
someString.length - 1 no matter how long the string is. The second to last character will be
someString.length - 2, the third
someString.length - 3, and so on.
Strings have a length property which represents the number of characters in the string. Strings have indices starting at 0 up to the length of the string -1. So if I have the following string:
var myString = 'abc',
myString.length; // this is 3
If I want the last character of myString (the ‘c’), I would reference myString[myString.length - 1]. If I want the 2nd to last character (the ‘b’), then I reference myString[myString.length - 2].
Ok i understand now, thank you both.