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 |
🏠 Go back to the homepage
How do you find a character in a string, using JavaScript?
How do you find a character in a string, using JavaScript?
You have one easy way.
Every string has an includes[]
method that accepts one [or more] characters.
This method returns true
if the string contains the character, and false
if not:
'a nice string'.includes['a'] //true
'a nice string'.includes['b'] //false
If you need to find the exact position of the letter in the string, however, you need to use the indexOf[]
method:
'a nice string'.indexOf['a'] //0
'a nice string'.indexOf['c'] //4
If there are more than one occurrence, this method returns the position of the first one it finds, starting from the left.
There's a few parts to what you're trying to accomplish. You've already gotten your string:
const myString = someDOMFunction[]; // 'This is my string'
Next you need to find the position or "index" of the first 's'
in that string:
const sIndex = myString.indexOf['s']; // 3
you can then use that index to split the string into two parts:
const leftPart = myString.substr[0, sIndex]; // 'Thi'
const rightPart = myString.substr[sIndex + 1]; // ' is my string'
Finally you can build a string of HTML [and CSS] which includes those two parts, using template string syntax:
const html = `${leftPart}s${rightPart}`;
... and then add that HTML back onto the page, for instance:
$['#someElement'].innerHTML = html;
Check if String contains a Character #
Use the String.includes[]
method to check if a string contains a character, e.g. if [str.includes[char]] {}
. The include[]
method will return true
if the string contains the provided character, otherwise false
is returned.
Copied!
const str = 'Hello world'; const char = 'e'; console.log[str.includes[char]]; // 👉️ true if [str.includes[char]] { // 👉️ string contains the character }
We used the String.includes method to determine if the character is contained in the string.
This works with special characters as well, e.g. emojis:
Copied!
const str = 'Hello world 🐔'; const char = '🐔'; console.log[str.includes[char]]; // 👉️ true if [str.includes[char]] { // 👉️ string contains the character }
If you need to perform a case insensitive check, for whether a character is contained in a string, convert the string and the character to lowercase when calling String.includes
.
Copied!
const str = 'Hello world'; const char = 'L'; // 👇️ true console.log[str.toLowerCase[].includes[char.toLowerCase[]]]; if [str.toLowerCase[].includes[char.toLowerCase[]]] { // 👉️ string contains the character }
We performed a case-insensitive check by converting the string and the character to lowercase when calling the includes
method.
Alternatively, you can use the String.indexOf
method.
Use the String.indexOf[]
method to check if a string contains a character, e.g. if [str.indexOf[char] !== -1] {}
. The indexOf
method will return the index of the character in the string or -1
if the character is not contained in the string.
Copied!
const str = 'Hello world'; const char = 'l'; // 👇️ 2 console.log[str.indexOf[char]]; if [str.indexOf[char] !== -1] { // 👉️ string contains the character }
We used the indexOf method to check if a string contains a character.
If there is no match, the String.indexOf
method returns -1
.
Our if
statement checks if the indexOf
returned a value other than -1
. If it did, the character is contained in the string.
Which approach you pick is a matter of personal
preference. I'd go with the includes
method as I find it more direct and intuitive.
Further Reading #
- Check if String contains Special Characters in JavaScript
- Remove Special Characters from a String in JavaScript
- Check if String contains only Letters and Numbers in JS
- Check if String contains only Letters and Spaces in JS
- Check if String contains only Latin Letters in JavaScript
- Check if String contains only Digits in JavaScript
- Check if String contains only Spaces in JavaScript
- Check if String contains only Letters in JavaScript
- Check if a String is all Uppercase in JavaScript
- Check if a String ends with a Number in JavaScript
- Check if String starts with Substring in JavaScript
- Check if String contains any Letter in JavaScript