forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 2
JS String Prototype CharAt
Quincy Larson edited this page Aug 20, 2016
·
1 revision
The charAt()
method returns the specified character from a string.
str.charAt(index)
index
An integer between 0 and 1-less-than the length of the string.
Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string called stringName
is stringName.length - 1
. If the index you supply is out of range, JavaScript returns an empty string.
var anyString = 'Brave new world';
console.log("The character at index 0 is '" + anyString.charAt(0) + "'"); // 'B'
console.log("The character at index 1 is '" + anyString.charAt(1) + "'"); // 'r'
console.log("The character at index 2 is '" + anyString.charAt(2) + "'"); // 'a'
console.log("The character at index 3 is '" + anyString.charAt(3) + "'"); // 'v'
console.log("The character at index 4 is '" + anyString.charAt(4) + "'"); // 'e'
console.log("The character at index 999 is '" + anyString.charAt(999) + "'"); // ''
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(str.charAt(str.length - 1));
// Output: Z
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links