Files

26 lines
795 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Use Bracket Notation to Find the Last Character in a String
---
# Use Bracket Notation to Find the Last Character in a String
2018-10-12 15:37:13 -04:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
Consider the following string:
```javascript
var str = "Coding";
```
2018-10-12 15:37:13 -04:00
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:
```javascript
var lastChar = str[str.length - 1]; // This is 6 - 1, which is 5
```
2018-10-12 15:37:13 -04:00
This will be 'g'.