JavaScript provides a variety of built-in methods to work with strings — allowing you to extract, modify, search, and format text with ease. Below are some of the most commonly used core string methods.
slice() MethodThe slice() method extracts a part of the string based on the given starting index and ending index and returns a new string.
// Define a string variable let A = 'Intricate for Devo';// Use the slice() method to extract a substring let b = A.slice(0, 9); let c = A.slice(10, 13); let d = A.slice(14);
// Output the value of variable console.log(b); // Output: Intricate console.log(c); // Output: for console.log(d); // Output: Devo
substring() Methodsubstring() returns the part of the given string from the start index to the end index. Indexing starts from zero (0).
// Define a string variable let str = "Mind, Power, Soul";// Use the substring() method to extract a substring let part = str.substring(6, 11);
// Output the value of variable console.log(part); // Output: Power
substr() Methodsubstr() returns the specified number of characters from the specified index from the given string. It extracts a part of the original string.
// Define a string variable 'str' let str = "Mind, Power, Soul";// Use the substr() method to extract a substring let part = str.substr(6, 5);
// Output the value of variable console.log(part); // Output: Power
replace() Methodreplace() replaces a part of the given string with another string or a regular expression. The original string will remain unchanged.
// Define a string variable 'str' let str = "Mind, Power, Soul";// Use the replace() method to replace the substring let part = str.replace("Power", "Space");
// Output the resulting string after replacement console.log(part); // Output: Mind, Space, Soul
replaceAll() MethodreplaceAll() returns a new string after replacing all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.
// Define a string variable 'str' let str = "Mind, Power, Power, Soul";// Use the replaceAll() method to replace all occurrences // of "Power" with "Space" in the string 'str' let part = str.replaceAll("Power", "Space");
// Output the resulting string after replacement console.log(part); // Output: Mind, Space, Space, Soul
toUpperCase() MethodtoUpperCase() converts all the characters present in the String to upper case and returns a new String with all characters in upper case. This method accepts a single parameter stringVariable string that you want to convert in upper case.
// Define a string variable let idStr = 'ID ';// Define another string variable let devo = 'stands-for-IntricateDevo';
// Convert the string 'devo' to uppercase using the toUpperCase() method console.log(devo.toUpperCase()); // Output: STANDS-FOR-INTRICATEDEVO
toLowerCase() MethodtoLowerCase() converts all the characters present in the string to lowercase and returns a new string with all the characters in lowercase.
// Define a string variable let idStr = 'ID ';// Define a string variable let devo = 'stands-for-IntricateDevo';
// Convert the string 'devo' to lowercase using the toLowerCase() method console.log(devo.toLowerCase()); // Output: stands-for-intricatedevo
concat() Methodconcat() combines the text of two strings and returns a new combined or joined string. To concatenate two strings, we use the concat() method on one object of string and send another object of string as a parameter.
let idStr = 'ID '; let devo = 'stands for IntricateDevo';// Accessing concat method on an object of String passing another object as a parameter console.log(idStr.concat(devo)); // Output: ID stands for IntricateDevo
trim() Methodtrim() is used to remove white spaces from both ends of the given string. This method returns a new string with removed white spaces.
let idStr = 'ID '; let devo = 'stands-for-IntricateDevo';// Storing new object of string with removed white spaces let newIdStr = idStr.trim();
// Old length vs New length console.log(idStr.length); // Output: 6 console.log(newIdStr.length); // Output: 2
trimStart() MethodtrimStart() removes whitespace from the beginning of a string. The value of the original string is not modified.
// Define a string variable let str = " Soul";// Output the original value of the string console.log(str); // Output: " Soul"
// Use the trimStart() method to remove leading whitespace let part = str.trimStart();
// Output the resulting string console.log(part); // Output: "Soul"
trimEnd() MethodtrimEnd() removes white space from the end of a string. The value of the original string is not modified.
// Define a string variable let str = "Soul ";// Output the original value of the string console.log(str); // Output: "Soul "
// Use the trimEnd() method to remove trailing whitespace let part = str.trimEnd();
// Output the resulting string console.log(part); // Output: "Soul"
padStart() MethodpadStart() pads a string with another string until it reaches the given length. The padding is applied from the left end of the string.
// Define a string variable let stone = "Soul";// Use the padStart() method to add padding characters stone = stone.padStart(9, "Mind ");
// Output the resulting string after padding console.log(stone); // Output: Mind Soul
padEnd() MethodpadEnd() pads a string with another string until it reaches the given length. The padding is applied from the right end of the string.
// Define a string variable let stone = "Soul";// Use the padEnd() method to add padding characters stone = stone.padEnd(10, " Power");
// Output the resulting string after padding console.log(stone); // Output: Soul Power
charAt() MethodcharAt() returns the character at the specified index. Strings in JavaScript have zero-based indexing.
let idStr = 'IntricateDevo'; let devo = 'IntricateDevo is the best platform to learn and\nexperience Computer Science.';// As string index starts from zero, it will return the first character of string console.log(idStr.charAt(0)); // Output: I console.log(devo.charAt(5)); // Output: c
charCodeAt() MethodcharCodeAt() returns a number that represents the Unicode value of the character at the specified index.
let idStr = 'IntricateDevo'; let devo = 'IntricateDevo is the best platform\nto learn and experience\nComputer Science.';// Return a number indicating Unicode value of character console.log(idStr.charCodeAt(0)); // Output: 73 console.log(devo.charCodeAt(5)); // Output: 99
split() Methodsplit() splits the string into an array of sub-strings. This method accepts a single parameter character on which you want to split the string.
let idStr = 'ID ' let devo = 'stands-for-IntricateDevo'// Split string on '-' console.log(devo.split('-')); // Output: [ 'stands', 'for', 'IntricateDevo' ]
In addition to the core methods covered above, JavaScript's String object comes with a comprehensive set of both instance and static methods for more advanced text manipulation. Below is a list of these methods for your reference.
These methods are called on an instance of a string (e.g., myString.toUpperCase()).
| Method | Description |
|---|---|
at() |
Returns the character at a specified index, allowing for positive and negative integers. |
anchor() |
(Deprecated) Creates an HTML anchor <a> element around the string. |
charAt() |
Returns the character at the specified index. |
charCodeAt() |
Returns the Unicode value of the character at the specified index. |
codePointAt() |
Returns a non-negative integer that is the Unicode code point value. |
concat() |
Joins two or more strings and returns a new, combined string. |
endsWith() |
Checks if a string ends with the characters of a specified string. |
includes() |
Checks if a string contains the specified characters. |
indexOf() |
Returns the index of the first occurrence of a specified value in a string. |
isWellFormed() |
Checks if the string contains any lone surrogates. |
lastIndexOf() |
Returns the index of the last occurrence of a specified value in a string. |
localeCompare() |
Compares two strings in the current locale. |
match() |
Searches a string for a match against a regular expression, and returns the matches. |
matchAll() |
Returns an iterator of all results matching a string against a regular expression. |
normalize() |
Returns the Unicode Normalization Form of the string. |
padEnd() |
Pads the current string from the end with a given string to create a new string of a specific length. |
padStart() |
Pads the current string from the start with a given string to create a new string of a specific length. |
repeat() |
Returns a new string with a specified number of copies of the original string. |
replace() |
Searches a string for a specified value or regular expression and returns a new string where the specified values are replaced. |
replaceAll() |
Returns a new string with all matches of a pattern replaced by a replacement. |
search() |
Searches a string for a specified value or regular expression and returns the position of the match. |
slice() |
Extracts a section of a string and returns it as a new string. |
split() |
Splits a string into an array of substrings. |
startsWith() |
Checks if a string begins with the characters of a specified string. |
substr() |
(Deprecated) Extracts a part of a string, beginning at a specified start position, and returns the specified number of characters. |
substring() |
Extracts the characters from a string, between two specified indices. |
toLocaleLowerCase() |
Converts a string to lowercase letters, according to the host's current locale. |
toLocaleUpperCase() |
Converts a string to uppercase letters, according to the host's current locale. |
toLowerCase() |
Converts a string to lowercase letters. |
toString() |
Returns the value of a String object. |
toUpperCase() |
Converts a string to uppercase letters. |
toWellFormed() |
Returns a string where all lone surrogates are replaced with the Unicode replacement character U+FFFD. |
trim() |
Removes whitespace from both ends of a string. |
trimEnd() |
Removes whitespace from the end of a string. |
trimStart() |
Removes whitespace from the beginning of a string. |
valueOf() |
Returns the primitive value of a String object. |
Symbol.iterator |
Returns an iterator object that iterates over the code points of the string. |
These methods are called directly on the String object itself (e.g., String.fromCharCode()).
| Method | Description |
|---|---|
String.fromCharCode() |
Creates a string from a sequence of UTF-16 code units. |
String.fromCodePoint() |
Creates a string from a sequence of code points. |
String.raw() |
A static method to get the raw string form of template literals, without processing escape sequences. |