Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding word starting with specific letter in JavaScript
Finding words that start with a specific letter is a common requirement in JavaScript applications. This guide shows different approaches to locate the first array element beginning with a specified character.
Problem Statement
We need to write a JavaScript function that takes an array of strings and a character, then returns the index of the first string starting with that character.
Using substring() Method
The substring() method extracts the first character for comparison:
const names = ['Naman', 'Kartik', 'Anmol', 'Rajat', 'Keshav', 'Harsh', 'Suresh', 'Rahul'];
const firstIndexOf = (arr = [], char = '') => {
for(let i = 0; i < arr.length; i++){
const el = arr[i];
if(el.substring(0, 1) === char){
return i;
};
};
return -1;
};
console.log(firstIndexOf(names, 'K')); // Found 'Kartik' at index 1
console.log(firstIndexOf(names, 'R')); // Found 'Rajat' at index 3
console.log(firstIndexOf(names, 'J')); // Not found, returns -1
1 3 -1
Using charAt() Method
The charAt() method provides a cleaner way to access the first character:
const findWordStartingWith = (arr, char) => {
for(let i = 0; i < arr.length; i++){
if(arr[i].charAt(0) === char){
return i;
}
}
return -1;
};
const fruits = ['apple', 'banana', 'cherry', 'orange', 'grape'];
console.log(findWordStartingWith(fruits, 'c')); // Found 'cherry'
console.log(findWordStartingWith(fruits, 'o')); // Found 'orange'
console.log(findWordStartingWith(fruits, 'z')); // Not found
2 3 -1
Using findIndex() Method
For a more functional approach, use the built-in findIndex() method:
const animals = ['cat', 'dog', 'elephant', 'fox', 'giraffe'];
const findByFirstLetter = (arr, char) => {
return arr.findIndex(word => word.charAt(0) === char);
};
console.log(findByFirstLetter(animals, 'd')); // Found 'dog'
console.log(findByFirstLetter(animals, 'f')); // Found 'fox'
console.log(findByFirstLetter(animals, 'z')); // Not found
1 3 -1
Case-Insensitive Search
To handle both uppercase and lowercase letters, convert to lowercase before comparison:
const countries = ['India', 'japan', 'Canada', 'brazil'];
const findCaseInsensitive = (arr, char) => {
return arr.findIndex(word => word.charAt(0).toLowerCase() === char.toLowerCase());
};
console.log(findCaseInsensitive(countries, 'j')); // Found 'japan'
console.log(findCaseInsensitive(countries, 'B')); // Found 'brazil'
console.log(findCaseInsensitive(countries, 'i')); // Found 'India'
1 3 0
Comparison
| Method | Performance | Readability | Browser Support |
|---|---|---|---|
substring(0, 1) |
Good | Moderate | Excellent |
charAt(0) |
Best | Good | Excellent |
findIndex() |
Good | Best | ES6+ |
Conclusion
Use charAt(0) for the best performance and readability when finding words starting with specific letters. The findIndex() method offers a more functional approach for modern JavaScript applications.
