Finding What Character Is at a Specific Location in a String

var s = "In The Darkness Bind Them"; s.charAt(7); //returns 'D' s[7]; // also returns 'D'

String objects provide the charAt(offset), which returns the character contained at the offset specified. Also, strings in JavaScript are basically arrays of characters, so you can get the character at a specific offset using stringName[offset]. Keep in mind that the offsets are always zero based, so their first character is at offset 0.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.