Examples
Get the first character in a string:
let text = "HELLO WORLD";
let letter = text.charAt[0];
Try it Yourself »
Get the second character in a string:
let text = "HELLO WORLD";
let letter = text.charAt[1];
Try it Yourself »
Get the last character in a string:
let text = "HELLO WORLD";
let letter = text.charAt[text.length-1];
Try it Yourself »
More examples below.
Definition and Usage
The charAt[]
method returns the character at a specified index [position] in a string.
The index of the first character is 0, the second 1, ...
Syntax
Parameters
Parameter | Description |
index | Optional. The index [position] of the character. Default = 0. |
Return Value
Type | Description |
String | The character at the specified index. Empty string [""] if the index is out of range. |
More Examples
Index out of range returns empty string:
let text = "HELLO WORLD";
let letter = text.charAt[15];
Try it Yourself »
Default index is 0:
let text = "HELLO WORLD";
let letter = text.charAt[];
Try it Yourself »
Invalid index converts to 0:
let text = "HELLO WORLD";
let letter = text.charAt[3.14];
Try it Yourself »
Browser Support
charAt[]
is an ECMAScript1 [ES1] feature.
ES1 [JavaScript 1997] is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
Examples
Extract a substring from text:
let text = "Hello world!";
let result = text.substring[1, 4];
Try it Yourself »
Start from position 2:
let result = text.substring[2];
Try it Yourself »
More examples below.
Definition and Usage
The substring[]
method extracts characters, between two indices [positions], from a string, and returns the substring.
The substring[]
method extracts characters from start to end [exclusive].
The substring[]
method does not change the original string.
If start is greater than end, arguments are swapped: [4, 1] = [1, 4].
Start or end values less than 0, are treated as 0.
Syntax
string.substring[start, end]
Parameters
Parameter | Description |
start | Required. Start position. First character is at index 0. |
end | Optional. End position [up to, but not including]. If omitted: the rest of the string. |
Return Value
Type | Description |
A string | A string containing the extracted characters. |
More Examples
If start is greater than end, parameters are swapped:
let result = text.substring[4, 1];
Try it Yourself »
If "start" is less than 0, it will start from index 0:
let result = text.substring[-3];
Try it Yourself »
Only the first:
let result = text.substring[0, 1];
Try it Yourself »
Only the last:
let result = text.substring[text.length - 1];
Try it Yourself »
Browser Support
substring[]
is an ECMAScript1 [ES1] feature.
ES1 [JavaScript 1997] is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
How to process each letter of text [with benchmarks]
//jsperf.com/str-for-in-of-foreach-map-2
for
Classic and by far the one with the most performance. You should go with this one if you are planning to use it in a performance critical algorithm, or that it requires the maximum compatibility with browser versions.
for [var i = 0; i < str.length; i++] {
console.info[str[i]];
}
for...of
for...of is the new ES6 for iterator. Supported by most modern browsers. It is visually more appealing and is less prone to typing mistakes. If you are going for this one in a production application, you should be probably using a transpiler like Babel.
let result = '';
for [let letter of str] {
result += letter;
}
forEach
Functional approach. Airbnb approved. The biggest downside of doing it this way is the split[]
, that creates a new array to store each individual letter of the string.
Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.
// ES6 version.
let result = '';
str.split[''].forEach[letter => {
result += letter;
}];
or
var result = '';
str.split[''].forEach[function[letter] {
result += letter;
}];
The following are the ones I dislike.
for...in
Unlike for...of, you get the letter index instead of the letter. It performs pretty badly.
var result = '';
for [var letterIndex in str] {
result += str[letterIndex];
}
map
Function approach, which is good. However, map isn't meant to be used for that. It should be used when needing to change the values inside an array, which is not the case.
// ES6 version.
var result = '';
str.split[''].map[letter => {
result += letter;
}];
or
let result = '';
str.split[''].map[function[letter] {
result += letter;
}];