Additional String Methods in JavaScript
Introduction
Beyond the basic string methods, JavaScript offers many more useful methods to manipulate, inspect, and transform text efficiently. ✨
1️⃣ charAt()
Returns the character at a specific index in the string.
Code Snippet
const str = "Hello";
console.log(str.charAt(1)); // "e"2️⃣ charCodeAt()
Returns the Unicode value of the character at a specific index.
Code Snippet
const str = "A";
console.log(str.charCodeAt(0)); // 653️⃣ repeat()
Repeats the string a specified number of times.
Code Snippet
const str = "Hi! ";
console.log(str.repeat(3)); // "Hi! Hi! Hi! "4️⃣ substring()
Returns a part of the string between two indices.
Code Snippet
const str = "Hello World";
console.log(str.substring(0,5)); // "Hello"5️⃣ toLocaleUpperCase()
Converts string to uppercase according to locale rules.
Code Snippet
const str = "i";
console.log(str.toLocaleUpperCase('tr')); // "İ" (Turkish locale)6️⃣ toLocaleLowerCase()
Converts string to lowercase according to locale rules.
Code Snippet
const str = "I";
console.log(str.toLocaleLowerCase('tr')); // "ı" (Turkish locale)