freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../basic-javascript/use-bracket-notation-to-fin.../index.md

13 lines
688 B
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Use Bracket Notation to Find the Nth-to-Last Character in a String
---
## Use Bracket Notation to Find the Nth-to-Last Character in a String
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
Remember that the position of any character, is the <strong>length of the string, minus one, minus the number of characters after it</strong>. For example, if you are trying to find the third-to-last character of the following string:
var str = "Programming";
var secondToLastChar = str[str.length - 2]; // This is 'i'
As you can see, there is one extra character after 'n' (and that is 'g').