Syntax
str.length
str.charAt(index)
str.at(index)Examples
Length and Character Access
Getting a string's length and accessing individual characters.
const text = "Hello";
console.log(text.length); // 5
console.log(text.charAt(0)); // "H"
console.log(text[1]); // "e" - bracket access also works
console.log(text.charAt(10)); // "" - out of range returns empty stringat() with Negative Indices
Using at() to easily access characters from the end of the string.
const text = "JavaScript";
console.log(text.at(0)); // "J"
console.log(text.at(-1)); // "t" - last character
console.log(text.at(-2)); // "p" - second to last
// Compare with charAt(), which has no negative index support
console.log(text.charAt(text.length - 1)); // "t" - more verbose equivalentBest practices
- Use at(-1) instead of str[str.length - 1] for cleaner access to the last character
- Remember strings are immutable - "changing" a character actually means creating an entirely new string
- Use bracket notation (str[0]) or charAt() interchangeably for simple positive-index access - both work identically for valid indices
- Check .length before accessing indices in a loop to avoid undefined results from out-of-range access
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
slice(), substring(), substr()
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.split()
split() divides a string into an array of substrings based on a specified separator, which can be a literal string or a regular expression. It is the inverse operation of Array.prototype.join(). An optional second argument limits the number of resulting elements.
These three methods extract a portion of a string as a new string. slice() and substring() are similar, but slice() accepts negative indices (counting from the end) while substring() treats negative values as 0 and swaps arguments if start is greater than end. substr() (deprecated) uses a start index and a length rather than an end index.indexOf(), includes(), startsWith(), endsWith()
These methods search within a string for a substring. indexOf() returns the position of the first match (or -1). includes() returns a simple true/false. startsWith() and endsWith() check specifically whether the string begins or ends with a given substring. All are case-sensitive.replace() and replaceAll()
replace() returns a new string with the first match of a pattern replaced by a new value - or all matches, if the pattern is a regex with the global (g) flag. replaceAll(), introduced in ES2021, always replaces every occurrence without needing a regex, making simple find-and-replace-all operations more straightforward.split()
split() divides a string into an array of substrings based on a specified separator, which can be a literal string or a regular expression. It is the inverse operation of Array.prototype.join(). An optional second argument limits the number of resulting elements.