How to Iterate Over Characters of a String in JavaScript ? Last Updated : 12 Nov, 2024 Comments Improve Suggest changes Like Article Like Report There are several methods to iterate over characters of a string in JavaScript. 1. Using for LoopThe classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length.Syntaxfor (statement 1 ; statement 2 ; statement 3){ code here... }; JavaScript let str = "Hello"; for (let i = 0; i < str.length; i++) { console.log(str[i]); } OutputH e l l o 2. Using for...of LoopThe for...of loop is a modern way to iterate directly over the characters of a string without needing to use indexing.Syntaxfor ( variable of iterableObjectName) { // Code... } JavaScript let str = "Hello"; for (let char of str) { console.log(char); } OutputH e l l o 3. Using forEach() MethodThe forEach() method can be used on arrays, so we first split the string into an array of characters, then use forEach() to iterate.Syntaxarray.forEach(callback(element, index, arr), thisValue) JavaScript let str = "Hello"; str.split('').forEach((char, index) => { console.log(`${index}: ${char}`); }); Output0: H 1: e 2: l 3: l 4: o 4. Using charAt() Method with while LoopThe charAt() method returns the character at a given index. Combining it with a while loop allows us to iterate over the string by manually tracking the index.Syntaxlet index = 0; while (index < str.length) { let char = str.charAt(index); // code here... index++; } JavaScript let str = "Hello"; let index = 0; while (index < str.length) { console.log(str.charAt(index)); index++; } OutputH e l l o 5. Using reduce() MethodThe reduce() method can be used to iterate over a string by first splitting it into an array, then using the accumulator to concatenate or perform operations on each character.Syntax:string.split('').reduce((acc, char) => { // Process char return acc + char;}, '' ); JavaScript let str = "Hello"; let result = str.split('').reduce((acc, char) => acc + char, ''); console.log(result); // Outputs: "Hello" OutputHello 6. Using for...in LoopThe for...in loop allows us to iterate over the indices of the string, which we can then use to access each character.Syntaxfor (let index in str) { const char = str[index]; // code here... } JavaScript let str = "Hello"; for (let index in str) { console.log(str[index]); } OutputH e l l o Comment More infoAdvertise with us Next Article How to Iterate Over Characters of a String in JavaScript ? V vishalkumar2204 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1 4 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read Reverse a String in JavaScript We have given an input string and the task is to reverse the input string in JavaScript. Reverse a String in JavaScriptUsing split(), reverse() and join() MethodsThe split() method divides the string into an array of characters, reverse() reverses the array, and join() combines the reversed characte 1 min read JavaScript - Convert String to Title Case Converting a string to title case means capitalizing the first letter of each word while keeping the remaining letters in lowercase. Here are different ways to convert string to title case in JavaScript.1. Using for LoopJavaScript for loop is used to iterate over the arguments of the function, and t 4 min read JavaScript - Sort an Array of Strings Here are the various methods to sort an array of strings in JavaScript1. Using Array.sort() MethodThe sort() method is the most widely used method in JavaScript to sort arrays. By default, it sorts the strings in lexicographical (dictionary) order based on Unicode values.JavaScriptlet a = ['Banana', 3 min read How to Convert String to Camel Case in JavaScript? We will be given a string and we have to convert it into the camel case. In this case, the first character of the string is converted into lowercase, and other characters after space will be converted into uppercase characters. These camel case strings are used in creating a variable that has meanin 4 min read Extract a Number from a String using JavaScript We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console.Below are the methods to extract a number from string using JavaScript:Table of ContentUsing JavaScript match method with regExUsing 4 min read JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s 1 min read JavaScript - How to Get Character Array from String? Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. JavaScriptlet s = "Geeksf 2 min read JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read Like