Loop through an index of an array to search for a certain letter in JavaScript

We need to write a JavaScript function that takes an array of strings and a single character, then returns true if the character exists in any string within the array, false otherwise.

Problem Overview

The function should search through each string in the array to find if the specified character exists anywhere. This is a common pattern for filtering or validation tasks.

Example Implementation

Here's how to implement this search functionality:

const arr = ['first', 'second', 'third', 'last'];

const searchForLetter = (arr = [], letter = '') => {
    for(let i = 0; i < arr.length; i++){
        const el = arr[i];
        if(!el.includes(letter)){
            continue;
        };
        return true;
    };
    return false;
};

console.log(searchForLetter(arr, 's'));
console.log(searchForLetter(arr, 'x'));

Output

true
false

How It Works

The function loops through each string in the array using a for loop. For each string, it uses the includes() method to check if the target letter exists. If the letter is not found, continue skips to the next iteration. As soon as a match is found, the function returns true. If no matches are found after checking all strings, it returns false.

Alternative Approaches

Here are other ways to solve this problem:

// Using some() method
const searchWithSome = (arr, letter) => {
    return arr.some(str => str.includes(letter));
};

// Using find() method
const searchWithFind = (arr, letter) => {
    return arr.find(str => str.includes(letter)) !== undefined;
};

// Testing alternatives
const testArray = ['hello', 'world', 'javascript'];
console.log(searchWithSome(testArray, 'j'));  // true
console.log(searchWithFind(testArray, 'z'));  // false
true
false

Comparison of Methods

Method Performance Readability Early Exit
for loop with continue Good Medium Yes
Array.some() Good High Yes
Array.find() Good High Yes

Conclusion

The for loop approach with continue provides explicit control over the search process. For cleaner code, consider using Array.some() which is more functional and readable.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements