JavaScript String Programs
Handle text in JavaScript. Reverse strings, count vowels and words, change case and check palindromes, with every line of code explained and its output shown.
5 programs · open one, then the next
Start with String Length- 01String LengthBeginnerFind string length in JavaScript with .length, a loop and split. Count characters, count words and see why emoji count as 2. Explanation and output.Open example →js// Method 1: Using length property let str = "Hello World"; console.log("String:", str); console.log("Length:", str.length);
- 02Reverse a StringBeginnerReverse a string in JavaScript with split, reverse and join, plus loops, reduce, recursion and spread. Includes the emoji trap, with explanation and output.Open example →js// Method 1: Using split, reverse, join let str = "Hello"; let reversed = str.split('').reverse().join(''); console.log("Original:", str);
- 03String Palindrome CheckBeginnerCheck a string palindrome in JavaScript: lowercase, strip spaces and symbols, then compare. "A man a plan a canal Panama" passes. Explanation and output.Open example →js// Method 1: Compare with reversed string function isPalindrome1(str) { str = str.toLowerCase().replace(/[^a-z0-9]/g, ''); return str === str.split('').reverse().join('');
- 04Anagram CheckIntermediateCheck if two strings are anagrams in JavaScript by sorting letters or counting them. "listen" and "silent" match. Full explanation, gotchas and output.Open example →js// Anagram: Same characters, different order // Examples: "listen" and "silent", "evil" and "vile" // Method 1: Sort and compare
- 05Count Vowels in StringBeginnerCount vowels in a string in JavaScript with loops, filter, reduce and regex. Also counts each vowel on its own, with a clear explanation and output.Open example →js// Method 1: Using for loop function countVowels1(str) { let count = 0; let vowels = 'aeiouAEIOU';