JavaScript String Methods
JavaScript strings are a sequence of characters, like words or sentences. They help us manage text in programs. JavaScript gives us many tools (called “methods”) to work with strings. These methods allow us to get parts of strings, combine them, change their appearance (like uppercase or lowercase), and much more.
Here’s the List of JavaScript String Methods:
Method | Definition | Example Code & Output |
---|---|---|
charAt() | Returns the character at a specific index in a string. |
let str = “Justin Bieber”;
console.log(str.charAt(2)); // Output: “s” |
charCodeAt() | Returns the Unicode of the character at a specific index. |
let str = “Justin Bieber”;
console.log(str.charCodeAt(2)); // Output: 115 (Unicode of ‘s’) |
concat() | Combines two or more strings and returns a new string. |
let str1 = “Justin “; let str2 = “Bieber”; console.log(str1.concat(str2)); // Output: “Justin Bieber” |
endsWith() | Checks if a string ends with a specified string/characters. |
let str = “Justin Bieber”; console.log(str.endsWith(“Bieber”)); // Output: true |
includes() | Checks if a string contains the specified string/characters. |
let str = “Justin Bieber”; console.log(str.includes(“Bieber”)); // Output: true |
indexOf() | Returns the index of the first occurrence of a substring. |
let str = “Justin Bieber”; console.log(str.indexOf(“Bieber”)); // Output: 7 |
lastIndexOf() | Returns the index of the last occurrence of a substring. |
let str = “Justin Bieber Bieber”; console.log(str.lastIndexOf(“Bieber”)); // Output: 13 |
match() | Retrieves matches of a string against a regular expression. |
let str = “Justin123Bieber”; console.log(str.match(/\d+/)); // Output: [“123”] |
matchAll() | Returns an iterator of all matched results. |
let str = “Justin Bieber”; console.log([…str.matchAll(/i/g)]); // Output: [[“i”], [“i”]] |
padStart() | Pads the current string with another string at the start. |
let str = “Bieber”; console.log(str.padStart(10, “Justin “)); // Output: “Justin Bieber” |
padEnd() | Pads the current string with another string at the end. |
let str = “Justin”; console.log(str.padEnd(12, ” Bieber”)); // Output: “Justin Bieber” |
repeat() | Returns a new string with a specified number of copies. |
let str = “Justin “; console.log(str.repeat(3)); // Output: “Justin Justin Justin ” |
replace() | Replaces a substring with a new substring. |
Table Bodylet str = “Justin Bieber”; console.log(str.replace(“Bieber”, “Timberlake”)); // Output: “Justin Timberlake” |
replaceAll() | Replaces all matches of a substring with a new substring. |
let str = “Justin Bieber Bieber”; console.log(str.replaceAll(“Bieber”, “Timberlake”)); // Output: “Justin Timberlake Timberlake” |
search() | Searches for a match between a string and a regular expression. |
let str = “Justin Bieber”; console.log(str.search(“Bieber”)); // Output: 7 |
slice() | Extracts a part of a string and returns it as a new string. |
let str = “Justin Bieber”; console.log(str.slice(7)); // Output: “Bieber” |
split() | Splits a string into an array of substrings. |
let str = “Justin Bieber”; console.log(str.split(” “)); // Output: [“Justin”, “Bieber”] |
startsWith() | Checks if a string starts with the specified string. |
let str = “Justin Bieber”; console.log(str.startsWith(“Justin”)); // Output: true |
substring() | Extracts the characters between two indices. |
let str = “Justin Bieber”; console.log(str.substring(0, 6)); // Output: “Justin” |
toLowerCase() | Converts a string to lowercase letters. |
let str = “Justin Bieber”; console.log(str.toLowerCase()); // Output: “justin bieber” |
toUpperCase() | Converts a string to uppercase letters. |
let str = “Justin Bieber”; console.log(str.toUpperCase()); // Output: “JUSTIN BIEBER” |
trim() | Removes whitespace from both ends of a string. |
let str = ” Justin Bieber “; console.log(str.trim()); // Output: “Justin Bieber” |
trimStart() | Removes whitespace from the beginning of a string. |
let str = ” Justin”; console.log(str.trimStart()); // Output: “Justin” |
trimEnd() | Removes whitespace from the end of a string. |
let str = “Bieber “; console.log(str.trimEnd()); // Output: “Bieber” |
valueOf() | Returns the primitive value of a String object. |
let str = new String(“Justin Bieber”); console.log(str.valueOf()); // Output: “Justin Bieber” |
localeCompare() | Compares two strings in the current locale. |
let str1 = “Justin”; let str2 = “Bieber”; console.log(str1.localeCompare(str2)); // Output: 1 (because “Justin” > “Bieber”) |
fromCharCode() | Converts Unicode values to characters. |
console.log(String.fromCharCode(74, 117, 115, 116, 105, 110)); // Output: “Justin” |
normalize() | Returns the Unicode Normalization Form of a string. |
let str = “Bieber\u0301”; console.log(str.normalize(“NFC”)); // Output: “Biéber” (normalized accent) |
Key Notes
• substr() has been deprecated and is replaced by slice() or substring().
• Methods like charAt(), charCodeAt(), replace(), and match() are among the most commonly used for basic string operations.
Remember
JavaScript string methods help you manipulate text, whether changing the case of letters, replacing parts of text, or extracting specific sections. These simple tools are useful for all types of programming tasks that involve text.
Course Video
YouTube Reference :
Practice Scenarios
Scenario 1: String Methods - Changing Case
Objective: Demonstrate the use of string methods to change the case of a string to uppercase and lowercase.
Expected Output: A console output showing the original string and its uppercase and lowercase versions.
Output
Scenario 2: Finding Substrings
Objective: Use the indexOf method to find the position of a substring within a string.
Expected Output: A console output showing the index of the substring and whether it exists in the original string.
phrase = “Learning JavaScript is fun!”
Output
Scenario 3: String Slicing
Objective: Use the slice method to extract a string portion.
Expected Output: A console output showing the original string and the sliced portions.
Output
Scenario 4: String Replacement
Objective: Use the replace method to replace part of a string with another substring.
Expected Output: A console output showing the original string and the modified string after replacement.
Output
Scenario 5: String Splitting
Objective: Use the split method to convert a string into an array of substrings based on a specified delimiter.
Expected Output: A console output showing the original string and the resulting array.
Output
Scenario 6: String Trimming
Objective: Use the trim method to remove whitespace from both ends of a string.
Expected Output: A console output showing the original string with whitespace and the trimmed version.
Output
Scenario 7: Repeating Strings
Objective: Use the repeat method to create a string that repeats a given substring multiple times.
Expected Output: A console output showing the repeated string.
baseString = “JavaScript! “
Output
Scenario 8: Checking String Start and End
Objective: Use the startsWith and endsWith methods to check if a string starts or ends with a specific substring.
Expected Output: A console output showing whether the string starts or ends with the given substring.
phrase = “JavaScript is fun!”
Output
Scenario 9: String Padding
Objective: Use the padStart and padEnd methods to add padding to a string to reach a certain length.
Expected Output: A console output showing the original string and its padded versions.
Output
Scenario 10: Checking for Substrings
Objective: Use the includes method to check if a string contains a specific substring.
Expected Output: A console output showing whether the substring is found in the string.
sentence = “Learning JavaScript is awesome!”
Output
Scenario 11: Extracting Characters
Objective: Use the charAt and charCodeAt methods to extract a specific character from a string and find its Unicode value.
Expected Output: A console output showing the character and its Unicode value.
word = “JavaScript”
Output
Scenario 12: Replacing All Occurrences
Objective: Use the replaceAll method to replace all occurrences of a substring within a string.
Expected Output: A console output showing the original string and the modified string with all occurrences replaced.
Output
Scenario 13: Concatenating Strings
Objective: Use the concat method to join two or more strings together.
Expected Output: A console output showing the concatenated string.
• greeting = “Hello,
• name = “John!”
Output
Scenario 14: Accessing Characters Using Bracket Notation
Objective: Access individual characters of a string using bracket notation.
Expected Output: A console output showing the characters accessed using their indices.
Output
language = “JavaScript”
Scenario 15: Reversing a String (Manual)
Objective: Use string methods and array methods to manually reverse a string.
Expected Output: A console output showing the original and reversed strings.
Output
Scenario 16: Replacing the First Occurrence of a Character
Objective: Use the replace method to replace only the first occurrence of a specific character in a string.
Expected Output: A console output showing the original string and the modified string with the first occurrence replaced.
Output
JavaScript string methods, such as .toUpperCase(), .indexOf(), and .slice(), are built-in functions that allow for string manipulation.
These methods convert a string to uppercase or lowercase. For example, .toUpperCase() changes “hello” to “HELLO”.
Use .indexOf() or .lastIndexOf() to locate a substring.
The .slice() and .substring() methods are key to extracting specific portions of a string.
The .replace() method modifies the first occurrence of a substring, while .replaceAll() updates all occurrences.
The .split() method divides a string into an array based on a specified separator.
The .trim() method removes whitespace from both ends of a string.
Use .startsWith() and .endsWith() methods to verify the beginning or end of a string.
The .repeat() method repeats a string multiple times.
Yes, string methods in JavaScript are case-sensitive.
You can learn to manipulate strings using techniques like concatenation, slicing, and searching in our comprehensive guide.
The best JavaScript string functions tutorial simplifies complex string operations and ensures clarity with real-world use cases.
Common properties include .length to measure string length and .constructor to access a string’s constructor.
Yes, the tutorial includes a free video in Hindi, making it easier to grasp JavaScript string methods.
Absolutely! Advanced topics include regex integration and chaining methods.
Yes, free resources and practice exercises are available.
Beginners should start with techniques like splitting, trimming, and concatenating strings.
Using functions like .slice() and .replaceAll() enhances coding efficiency by reducing manual string manipulation.
Understanding JavaScript string properties is essential for debugging and optimizing code with precise string data.