
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Loop through an index of an array to search for a certain letter in JavaScript
We are required to write a JavaScript function that takes in an array of strings as the first argument and a single character as the second argument.
The function should return true if the character specified by second argument exists in any of the strings of the array, false otherwise.
Example
The code for this will be −
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
And the output in the console will be −
true false
- Related Articles
- How to use for...in statement to loop through an Array in JavaScript?
- Loop through an array in Java
- How to use for each loop through an array in Java?
- How to loop through an array in Java?
- How to search for an element in JavaScript array?
- JavaScript Program for Equilibrium index of an array
- Recursively loop through an array and return number of items with JavaScript?
- How to loop through all the elements of an array in C#?
- How do I search through an array using a string, which is split into an array with JavaScript?
- How to search documents through an integer array in MongoDB?
- Create an index for text search in MongoDB
- JavaScript Program for Search an element in a sorted and rotated array
- Search in an array with Binary search using JavaScript
- Looping through an array in Javascript
- Sort by index of an array in JavaScript

Advertisements