• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / for each char in string – How to Loop Over Characters of String in JavaScript

for each char in string – How to Loop Over Characters of String in JavaScript

September 23, 2022 Leave a Comment

To loop over the characters of a string in JavaScript, we can use a for loop, the JavaScript charAt() method, and the length property to loop over each character in the following way.

var example_string = "example";

for( var i=0; i<example_string.length; i++ ){
  console.log(example_string.charAt(i));
}

#Output:
e
x
a
m
p
l
e

When working with strings, the ability to work with the characters individually is valuable.

You can loop over the characters of a string variable in JavaScript easily.

String objects are iterable in JavaScript and therefore you can loop over a string.

To loop over the characters of a string in JavaScript, you can use a for loop and loop over each character in the following way.

var example_string = "example";

for( var i=0; i<example_string.length; i++ ){
  console.log(example_string.charAt(i));
}

#Output:
e
x
a
m
p
l
e

Examples of Loop Over String in JavaScript

Depending on the situation, you might find it useful to want to loop over the characters of a string.

In JavaScript, we can easily find if a string contains certain characters by looping over each character of the string and seeing if it is one of the given characters or not.

Below is a function that will check if a string has certain characters or not for you in a string using JavaScript. The function will take two strings. We will use nested for loops to check if any character in the second string is contained in the first. If one is found, we will return true. Otherwise, false.

function containsCertainChars(theString, chars){
  for( var i=0; i<theString.length; i++ ){
    for( var k=0; k<chars.length; k++ ){
      if(theString.charAt(i) === chars.charAt(k)){
        return true;
      }
    }
  }
  return false;
};

console.log(containsCertainChars("Hello World!", "H"));
console.log(containsCertainChars("Hello World!", "olz"));
console.log(containsCertainChars("Hello World!", "z"));

#Output:
true
true
false

Another example of using a for loop to check each char in a string is if we want to check if a string contains uppercase letters in JavaScript.

We can check if a string contains uppercase characters in JavaScript by checking each letter to see if that letter is uppercase in a loop. We will make use of the toUpperCase() and charAt() methods.

Here is our function that will check if there are any uppercase letters in a string.

function checkUppercase(str){
    for (var i=0; i<str.length; i++){
      if (str.charAt(i) == str.charAt(i).toUpperCase() && str.charAt(i).match(/[a-z]/i)){
        return true;
      }
    }
    return false;
};

console.log(checkUppercase("all letters here are lowercase"));
console.log(checkUppercase("We Have some uppercase Letters in this One."));

#Output:
false
true

Hopefully this article has been useful for you to learn how to use a JavaScript for loop to loop over each char in a string.

Other Articles You'll Also Like:

  • 1.  Adding an Underline with JavaScript
  • 2.  Using the getElementsByClassName Method in JavaScript
  • 3.  How to Get the First Character of a String in JavaScript
  • 4.  Using JavaScript to Get Today’s Date
  • 5.  Using JavaScript to Convert Month Number to Name
  • 6.  getDate JavaScript – Get the Current Day of the Month in JavaScript
  • 7.  JavaScript onkeyup – How to Use onkeyup Event with JavaScript
  • 8.  Grouping By Multiple Properties and Summing Using Javascript
  • 9.  Using JavaScript to Remove All Non-Alphanumeric Characters From a String
  • 10.  How to Change an Image on Hover Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy