JavaScript Notes
Solve JavaScript string programs: palindrome, anagram, reverse, capitalize, count vowels, remove duplicates, and common coding interview string problems with complete solutions.
How to Use This Guide
For each program:
- Understand the problem clearly
- Think of the approach before looking at the code
- Read the solution with comments
- Verify with the output
- Try the challenge variation yourself
Program 2 — Check Palindrome
A palindrome reads the same forwards and backwards.
true false true true
Program 3 — Count Vowels and Consonants
{
vowels: 5,
consonants: 9,
vowelChars: 'aaae',
consonantChars: 'vScrptMstr'
}Program 4 — Check Anagram
Two words are anagrams if they contain the same characters in any order.
true false true true
Program 5 — Capitalize First Letter of Each Word (Title Case)
The Javascript Master Course Hello World From Wohotech
Program 6 — Convert to camelCase
helloWorld getUserById backgroundColor firstNameLastName
Program 7 — Count Word Frequency
{
the: 3,
quick: 1,
brown: 1,
fox: 2,
jumps: 1,
over: 1,
lazy: 1,
dog: 1
}
Most common: [ [ 'the', 3 ], [ 'fox', 2 ], [ 'quick', 1 ] ]Program 8 — Remove Duplicate Characters
programing abcd javscrip
Program 9 — Truncate String with Ellipsis
function truncate(str, maxLength, suffix = "...") {
if (str.length <= maxLength) return str;
return str.slice(0, maxLength - suffix.length) + suffix;
}
// Word-aware truncation (don't cut mid-word)
function truncateWords(str, maxLength) {
if (str.length <= maxLength) return str;
return str.slice(0, str.lastIndexOf(" ", maxLength)) + "...";
}
const long = "JavaScript is a versatile programming language used everywhere";
console.log(truncate(long, 30));
console.log(truncateWords(long, 30));JavaScript is a versatile pro... JavaScript is a versatile...
Program 10 — Find the Longest Word
jumps [ 'love', 'cool', 'code' ]
Program 11 — Compress / Run-Length Encode a String
a3b3c2d2 a2b2c2 abcd
Program 12 — Extract Numbers from String
[ 1, 150, 2, 299.5, 44.95 ] Total: 497.45
Program 13 — Generate URL Slug
javascript-master-course-2026 how-to-use-react-nodejs top-10-es6-features-what-you-need
Program 14 — Caesar Cipher (Encrypt/Decrypt)
Original: Hello JavaScript Encrypted: Khoor MdydVfulsw Decrypted: Hello JavaScript
Program 15 — Validate Email (Basic)
user@example.com: true invalid-email: false user@.com: false user@domain.co.in: true @nodomain.com: false user@domain: false
Common Patterns Summary
| Problem | Core Technique |
|---|---|
| Reverse string | split("").reverse().join("") |
| Palindrome check | Reverse and compare |
| Anagram check | Sort characters and compare |
| Capitalize words | split(" ").map(w => w[0].toUpperCase() + w.slice(1)).join(" ") |
| camelCase | replace(/[_\-\s]+(\w)/g, (_, c) => c.toUpperCase()) |
| Word frequency | reduce into object |
| Remove duplicates | [...new Set(str)].join("") |
| Find pattern | match(/regex/g) |
| Replace pattern | replace(/regex/g, replacement) |
| Slug generation | .toLowerCase().replace(/\s+/g, "-") |
Interview Questions 🎯
Q1. Write a function to check if a string is a palindrome. > Reverse the string and compare: str === str.split("").reverse().join(""). For real-world use, normalize case and remove non-alphanumeric characters first.
Q2. How do you check if two strings are anagrams? > Sort both strings' characters and compare: str1.split("").sort().join("") === str2.split("").sort().join("").
Q3. How do you count the occurrences of a character in a string? > str.split(char).length - 1 or (str.match(new RegExp(char, "g")) || []).length.
Q4. How do you reverse words in a sentence (not characters)? > sentence.split(" ").reverse().join(" ").
Q5. How do you find the most frequently occurring character? > Build a frequency object with reduce, then find the key with the highest value using Object.entries() and sort or reduce.
Q6. What is run-length encoding? > Compressing repeated characters: "aaabbb" → "a3b3". Count consecutive identical characters and store count + character.
Q7. How do you convert a string to title case? > .split(" ").map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join(" ").
Key Takeaways 🏁
split("").reverse().join("")is the classic reverse string idiom- Sorting characters is the cleanest way to check anagrams
reduceinto an object is the pattern for word/character frequencynew Set()gives you unique characters efficiently for deduplicationmatch(/regex/g)returns an array of all matches — powerful for extraction- String problems in interviews usually combine split, map, filter, reduce, and join
- Practice these 15 programs and you'll be ready for most string-based coding questions
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript String Programs - Practice Problems with Solutions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, strings, string, programs
Related JavaScript Master Course Topics