A string in JavaScript is a sequence of characters used to represent text.
β
Ways to create strings
let str1 = "Hello";
let str2 = 'World';
let str3 = `Hello World`; // Template literal
String Methods
πΉ 1. length
Description: Returns the number of characters in a string.
let str = "Hello";
console.log(str.length); // 5
πΉ 2. toUpperCase()
Description: Converts all characters to uppercase.
"hello".toUpperCase(); // "HELLO"
πΉ 3. toLowerCase()
Description: Converts all characters to lowercase.
"HELLO".toLowerCase(); // "hello"
πΉ 4. trim()
Description: Removes whitespace from both ends.
" hi ".trim(); // "hi"
πΉ 5. trimStart() / trimEnd()
Description: Removes whitespace from start or end only.
" hi".trimStart(); // "hi"
"hi ".trimEnd(); // "hi"
πΉ 6. includes(substring)
Description: Checks if string contains a substring β returns true/false.
"JavaScript".includes("Script"); // true
πΉ 7. startsWith()
Description: Checks if string starts with given value.
"Hello".startsWith("He"); // true
πΉ 8. endsWith()
Description: Checks if string ends with given value.
"Hello".endsWith("lo"); // true
πΉ 9. indexOf()
Description: Returns first index of substring, or -1 if not found.
"Hello".indexOf("l"); // 2
πΉ 10. lastIndexOf()
Description: Returns last occurrence index.
"Hello".lastIndexOf("l"); // 3
πΉ 11. slice(start, end)
Description: Extracts part of string (supports negative index).
"JavaScript".slice(0, 4); // "Java"
"Hello".slice(-2); // "lo"
πΉ 12. substring(start, end)
Description: Similar to slice() but does not accept negative index.
"JavaScript".substring(4, 10); // "Script"
πΉ 13. substr(start, length) β οΈ Deprecated
Description: Extracts substring using length (not recommended now).
"Hello".substr(1, 3); // "ell"
πΉ 14. replace(search, value)
Description: Replaces first match.
"Hello World".replace("World", "JS"); // "Hello JS"
πΉ 15. replaceAll()
Description: Replaces all occurrences.
"a-b-c".replaceAll("-", ","); // "a,b,c"
πΉ 16. split(separator)
Description: Converts string into array.
"a,b,c".split(","); // ["a", "b", "c"]
πΉ 17. concat()
Description: Joins two or more strings.
"Hello".concat(" ", "World"); // "Hello World"
πΉ 18. charAt(index)
Description: Returns character at given index.
"Hello".charAt(1); // "e"
πΉ 19. charCodeAt(index)
Description: Returns Unicode value of character.
"A".charCodeAt(0); // 65
πΉ 20. repeat(n)
Description: Repeats string n times.
"Hi ".repeat(3); // "Hi Hi Hi "
πΉ 21. padStart(length, char)
Description: Pads string from start.
"5".padStart(3, "0"); // "005"
πΉ 22. padEnd(length, char)
Description: Pads string from end.
"5".padEnd(3, "0"); // "500"
πΉ 23. match(regex)
Description: Returns matches based on regex.
"abc123".match(/\d+/); // ["123"]
πΉ 24. matchAll(regex)
Description: Returns all matches (iterator).
[..."a1b2".matchAll(/\d/g)]; // ["1", "2"]
πΉ 25. search(regex)
Description: Returns index of first match.
"hello123".search(/\d/); // 5
πΉ 26. localeCompare()
Description: Compares two strings (useful for sorting).
"a".localeCompare("b"); // -1
πΉ 27. normalize()
Description: Normalizes Unicode characters.
"Γ©".normalize();
πΉ 28. valueOf()
Description: Returns primitive string value.
let str = new String("hello");
str.valueOf(); // "hello"
πΉ 29. toString()
Description: Converts object to string.
let str = new String("hello");
str.toString(); // "hello"
πΉ 30. Template Literals (`)
Description: Allows interpolation and multi-line strings.
let name = "John";
console.log(`Hello ${name}`);
β‘ Quick Categorization
π Search Methods
includes(), indexOf(), search()
βοΈ Extract Methods
slice(), substring(), substr()
π Modify Methods
replace(), replaceAll(), trim()
π§ Utility Methods
split(), concat(), repeat()
π‘ Case Methods
toUpperCase(), toLowerCase()
π₯ 20 JavaScript String Programs (with Solutions)
β
1. Reverse a string
let str = "hello";
let reversed = str.split("").reverse().join("");
console.log(reversed);
β
2. Check palindrome
let str = "madam";
let result = str === str.split("").reverse().join("");
console.log(result);
β
3. Count vowels
let str = "hello";
let count = str.match(/[aeiou]/gi)?.length || 0;
console.log(count);
β
4. Count consonants
let str = "hello";
let count = str.match(/[^aeiou]/gi).length;
console.log(count);
β
5. Remove spaces
let str = "h e l l o";
console.log(str.replaceAll(" ", ""));
β
6. Convert first letter to uppercase
let str = "hello";
console.log(str.charAt(0).toUpperCase() + str.slice(1));
β
7. Find longest word
let str = "I love JavaScript programming";
let words = str.split(" ");
let longest = words.reduce((a, b) => a.length > b.length ? a : b);
console.log(longest);
β
8. Count words
let str = "Hello world JS";
console.log(str.split(" ").length);
β
9. Check substring exists
let str = "JavaScript";
console.log(str.includes("Script"));
β
10. Replace all vowels with *
let str = "hello";
console.log(str.replace(/[aeiou]/gi, "*"));
β
11. Convert string to array
let str = "hello";
console.log(str.split(""));
β
12. Remove duplicate characters
let str = "aabbcc";
let unique = [...new Set(str)].join("");
console.log(unique);
β
13. Count character frequency
let str = "hello";
let obj = {};
for (let ch of str) {
obj[ch] = (obj[ch] || 0) + 1;
}
console.log(obj);
β
14. Check anagram
let a = "listen";
let b = "silent";let result = a.split("").sort().join("") === b.split("").sort().join("");
console.log(result);
β
15. Capitalize each word
let str = "hello world";
let result = str.split(" ").map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
console.log(result);
β
16. Extract numbers from string
let str = "abc123xyz";
console.log(str.match(/\d+/)[0]);
β
17. Check if string contains only digits
let str = "12345";
console.log(/^\d+$/.test(str));
β
18. Repeat string N times
let str = "hi";
console.log(str.repeat(3));
β
19. Truncate string
let str = "JavaScript";
console.log(str.slice(0, 4));
β
20. Convert string to title case
let str = "javascript is fun";
let result = str.split(" ")
.map(w => w[0].toUpperCase() + w.slice(1))
.join(" ");
console.log(result);