Iterate Over Strings
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 2 of 65.
In JavaScript, strings are iterable objects, meaning you can loop through each character in a string. There are many ways to iterate over a string. we will cover a few of them here.
Using For Loop:
const text = "Hello";
for (let i = 0; i < text.length; i++) {
console.log(text[i]);
}Using For…of Loop:
const text = "World";
for (let char of text) {
console.log(char);
}Challenge
EasyCreate a function named countUniqueVowels that takes a string as an argument and returns the number of unique vowels in the string (vowels are a, e, i, o, u). The function should not be case-insensitive. (include also the uppercased vowels)
Cheat sheet
Strings are iterable objects in JavaScript, allowing you to loop through each character.
Using For Loop:
const text = "Hello";
for (let i = 0; i < text.length; i++) {
console.log(text[i]);
}Using For…of Loop:
const text = "World";
for (let char of text) {
console.log(char);
}Try it yourself
function countUniqueVowels(str) {
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
// Write code here
}
// Do not write anything outside functionThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys