9. Strings – JavaScript Programs

1. Find String Length

Methods used: .length

Program 1: Direct Length

				
					<script>
let s = "JavaScript";
document.write(s.length);
</script>

				
			

Program 2: Using Function

				
					<script>
function getLength(s) {
  return s.length;
}
document.write(getLength("Programming"));
</script>

				
			

Program 3: Length After Removing Spaces

				
					<script>
let s = "Java Script";
document.write(s.replaceAll(" ", "").length);
</script>

				
			

2. Reverse a String

Methods used: .length, indexing, .split(), .reverse(), .join()

Program 1: Using Loop

				
					<script>
let s = "hello";
let rev = "";
for (let i = s.length - 1; i >= 0; i--) {
  rev += s[i];
}
document.write(rev);
</script>

				
			

Program 2: Using Predefined Methods

				
					<script>
let s = "JavaScript";
document.w
</script>
				
			

Program 3: Function Based

				
					<script>
function reverseStr(s) {
  return s.split("").reverse().join("");
}
document.write(reverseStr("coding"));
</script>

				
			

3. Check Palindrome String

Methods used: .split(), .reverse(), .join(), .toLowerCase()

Program 1: Simple Palindrome Check

				
					<script>
let s = "madam";
let rev = s.split("").reverse().join("");
document.write(s === rev ? "Palindrome" : "Not Palindrome");
</script>

				
			

Program 2: Case-Insensitive Palindrome

				
					<script>
let s = "Level";
let str = s.toLowerCase();
document.write(str === str.split("").reverse().join("") ? "Palindrome" : "Not");
</script>

				
			

Program 3: Using Function

				
					<script>
function isPalindrome(s) {
  s = s.toLowerCase();
  return s === s.split("").reverse().join("");
}
document.write(isPalindrome("Radar"));
</script>

				
			

4. Count Vowels and Consonants

Methods used: .toLowerCase(), .includes()

Program 1: Using Loop

				
					<script>
let s = "javascript";
let v = 0, c = 0;

for (let i = 0; i < s.length; i++) {
  let ch = s[i].toLowerCase();
  if (ch >= 'a' && ch <= 'z') {
    if ("aeiou".includes(ch)) v++;
    else c++;
  }
}
document.write("Vowels: " + v + ", Consonants: " + c);
</script>

				
			

Program 2: Using Regex

				
					<script>
let s = "programming";
let vowels = s.match(/[aeiou]/gi).length;
let consonants = s.match(/[bcdfghjklmnpqrstvwxyz]/gi).length;
document.write(vowels + " " + consonants);
</script>

				
			

5. Convert String to Uppercase / Lowercase

Methods used: .toUpperCase(), .toLowerCase()

Program 1: Uppercase

				
					<script>
let s = "javascript";
document.write(s.toUpperCase());
</script>

				
			

Program 2: Lowercase

				
					<script>
let s = "JAVASCRIPT";
document.write(s.toLowerCase());
</script>

				
			

Program 3: Toggle Case

				
					<script>
let s = "JavaScript";
let result = "";

for (let ch of s) {
  result += ch === ch.toUpperCase()
    ? ch.toLowerCase()
    : ch.toUpperCase();
}
document.write(result);
</script>

				
			

6. Count Words in a String

Methods used: .split(), .trim()

Program 1: Using split

				
					<script>
let s = "JavaScript is easy";
document.write(s.split(" ").length);
</script>

				
			

Program 2: Handling Extra Spaces

				
					<script>
let s = "  Learn   JavaScript   Now ";
document.write(s.trim().split(/\s+/).length);
</script>

				
			

Program 3: Manual Count

				
					<script>
let s = "Practice makes perfect";
let count = 1;
for (let i = 0; i < s.length; i++)
  if (s[i] === " ") count++;
document.write(count);
</script>

				
			

7. Replace a Word in a String

Methods used: .replace(), .replaceAll()

Program 1: Replace First Occurrence

				
					<script>
let s = "I like JS";
document.write(s.replace("JS", "JavaScript"));
</script>

				
			

Program 2: Replace All Occurrences

				
					<script>
let s = "JS is fast. JS is popular.";
document.write(s.replaceAll("JS", "JavaScript"));
</script>

				
			

Program 3: Using Regex

				
					<script>
let s = "apple apple apple";
document.write(s.replace(/apple/g, "orange"));
</script>

				
			

8. Check Substring Existence

Methods used: .includes(), .indexOf(), .search()

Program 1: Using includes

				
					<script>
let s = "JavaScript";
document.write(s.includes("Script"));
</script>

				
			

Program 2: Using indexOf

				
					<script>
let s = "Programming";
document.write(s.indexOf("gram") !== -1 ? "Found" : "Not Found");
</script>

				
			

Program 3: Using search

				
					<script>
let s = "Hello World";
document.write(s.search("World") >= 0);
</script>

				
			

9. Capitalize First Letter of Each Word

Methods used: .split(), .charAt(), .slice(), .toUpperCase()

Program 1: Using split

				
					<script>
let s = "java script language";
let words = s.split(" ");
let result = "";

for (let w of words) {
  result += w.charAt(0).toUpperCase() + w.slice(1) + " ";
}
document.write(result);
</script>

				
			

Program 2: Without split

				
					<script>
let s = "learning javascript";
let res = "";
let cap = true;

for (let ch of s) {
  if (cap && ch !== " ") {
    res += ch.toUpperCase();
    cap = false;
  } else {
    res += ch;
  }
  if (ch === " ") cap = true;
}
document.write(res);
</script>

				
			

10. Remove Spaces from String

Methods used: .replace(), .replaceAll(), .split(), .join(), .trim()

Program 1: Remove All Spaces

				
					<script>
let s = "Java Script Basics";
document.write(s.replaceAll(" ", ""));
</script>

				
			

Program 2: Remove Leading & Trailing Spaces

				
					<script>
let s = "   Hello World   ";
document.write(s.trim());
</script>

				
			

Program 3: Using split & join

				
					<script>
let s = "Remove all spaces";
document.write(s.split(" ").join(""));
</script>

				
			

A. Programs using for…in with String

(for…in iterates over indexes)

Program 1. Print Each Character with Index

				
					<script>
let str = "JavaScript";

for (let i in str) {
  document.write("Index " + i + " : " + str[i] + "<br>");
}
</script>

				
			

Program 2. Count Characters in a String

				
					<script>
let str = "Programming";
let count = 0;

for (let i in str) {
  count++;
}
document.write("Length: " + count);
</script>

				
			

Program 3. Reverse a String using for…in

				
					<script>
let str = "hello";
let rev = "";

for (let i in str) {
  rev = str[i] + rev;
}
document.write(rev);
</script>

				
			

Program 4. Count Vowels using for…in

				
					<script>
let str = "javascript";
let vowels = 0;

for (let i in str) {
  if ("aeiou".includes(str[i])) {
    vowels++;
  }
}
document.write("Vowels: " + vowels);
</script>

				
			

B. Programs using for…of with String

(for…of iterates over characters directly – best for strings)

Program 5. Print Each Character

				
					<script>
let str = "Coding";

for (let ch of str) {
  document.write(ch + "<br>");
}
</script>

				
			

Program 6. Count Vowels and Consonants

				
					<script>
let str = "education";
let v = 0, c = 0;

for (let ch of str) {
  if ("aeiou".includes(ch)) v++;
  else c++;
}

document.write("Vowels: " + v + "<br>Consonants: " + c);
</script>

				
			

Program 7. Reverse a String using for…of

				
					<script>
let str = "hello";
let rev = "";

for (let ch of str) {
  rev = ch + rev;
}
document.write(rev);
</script>

				
			

Program 8. Check Palindrome using for…of

				
					<script>
let str = "madam";
let rev = "";

for (let ch of str) {
  rev = ch + rev;
}

document.write(str === rev ? "Palindrome" : "Not Palindrome");
</script>

				
			

Program 9. Remove Spaces using for…of

				
					<script>
let str = "Java Script Basics";
let result = "";

for (let ch of str) {
  if (ch !== " ") result += ch;
}

document.write(result);
</script>

				
			

C. Programs using forEach with String

(forEach works after converting string → array)

Program 10. Print Each Character using forEach

				
					<script>
let str = "Hello";

str.split("").forEach(function(ch) {
  document.write(ch + "<br>");
});
</script>

				
			

Program 11. Count Vowels using forEach

				
					<script>
let str = "javascript";
let count = 0;

str.split("").forEach(ch => {
  if ("aeiou".includes(ch)) count++;
});

document.write("Vowels: " + count);
</script>

				
			

Program 12. Convert String to Uppercase using forEach

				
					<script>
let str = "hello";
let result = "";

str.split("").forEach(ch => {
  result += ch.toUpperCase();
});

document.write(result);
</script>

				
			

Program 13. Reverse String using forEach

				
					<script>
let str = "coding";
let rev = "";

str.split("").forEach(ch => {
  rev = ch + rev;
});

document.write(rev);
</script>

				
			

Program 14. Count Words using forEach

				
					<script>
let str = "JavaScript is very easy";
let count = 0;

str.split(" ").forEach(word => {
  if (word !== "") count++;
});

document.write("Words: " + count);
</script>

				
			

D. Comparison Program (Very Important for Learners)

Program 15. Same Task using All Three Loops

				
					<script>
let str = "Loop";

// for...in
for (let i in str) document.write(str[i] + " ");
document.write("<br>");

// for...of
for (let ch of str) document.write(ch + " ");
document.write("<br>");

// forEach
str.split("").forEach(ch => document.write(ch + " "));
</script>