- Instant help with your JavaScript coding problems

Check if string contains a specific character using JavaScript

Question:
How to check if string contains a specific character using JavaScript?
Answer:
const containsCharacter = (text, character) => {
    if (!text) {
        return false;
    }
    return text.includes(character);
}
Description:

In modern JavaScript it is quite simple to check if a string contains a specific character using the includes method of the string.

The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate. 

Fortunately, includes also supports emojis:

const text = 'demo 😁 text';
const result = containsCharacter(text, '😁'); // true

 

Share "How to check if string contains a specific character using JavaScript?"
Related snippets:
Tags:
string, text, include, contain, character, emoji, javascript
Technical term:
Check if string contains a specific character using JavaScript