JavaScript String Methods (original) (raw)

Last Updated : 07 Mar, 2025

**JavaScript strings are the sequence of characters. They are treated as **Primitive data types. In JavaScript, strings are automatically converted to string objects when using **string methods on them. This process is called **auto-boxing. The following are methods that we can call on strings.

Code Examples of the Above Methods

**slice()

slice() extracts a part of the string based on the given stating-index and ending-index and returns a new string.

JavaScript `

// Define a string variable let A = 'Geeks for Geeks';

// Use the slice() method to extract a substring let b = A.slice(0, 5); let c = A.slice(6, 9); let d = A.slice(10);

// Output the value of variable console.log(b); console.log(c); console.log(d);

`

**substring()

substring() returns the part of the given string from the start index to the end index. Indexing starts from zero (0).

JavaScript `

// 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);

`

**substr()

substr() This method returns the specified number of characters from the specified index from the given string. It extracts a part of the original string.

JavaScript `

// Define a string variable 'str' let str = "Mind, Power, Soul";

// Use the substr() method to extract a substring f let part = str.substr(6, 5);

// Output the value of variable console.log(part);

`

**replace()

**replace() replaces a part of the given string with another string or a regular expression. The original string will remain unchanged.

JavaScript `

// 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);

`

**replaceAll()

**replaceAll() 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.

JavaScript `

// 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()

**toUpperCase() 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 single parameter **stringVariable string that you want to convert in upper case.

JavaScript `

// Define a string variable let gfg = 'GFG ';

// Define another string variable let geeks = 'stands-for-GeeksforGeeks';

// Convert the string 'geeks' to uppercase using the toUpperCase() method console.log(geeks.toUpperCase());

`

Output

STANDS-FOR-GEEKSFORGEEKS

**toLowerCase()

**toLowerCase() converts all the characters present in the so lowercase and returns a new string with all the characters in lowercase.

JavaScript `

// Define a string variable let gfg = 'GFG ';

// Define a string variable let geeks = 'stands-for-GeeksforGeeks';

// Convert the string 'geeks' to lowercase using the toLowerCase() method console.log(geeks.toLowerCase());

`

Output

stands-for-geeksforgeeks

**concat()

**concat() 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. This method accepts one argument. The variable contains text in double quotes or single quotes.

JavaScript `

let gfg = 'GFG '; let geeks = 'stands for GeeksforGeeks';

// Accessing concat method on an object // of String passing another object // as a parameter console.log(gfg.concat(geeks));

`

Output

GFG stands for GeeksforGeeks

**trim()

**trim() is used to remove either white spaces from the given string. This method returns a new string with removed white spaces. This method is called on a String object. This method doesn't accept any parameter.

JavaScript `

let gfg = 'GFG '; let geeks = 'stands-for-GeeksforGeeks';

// Storing new object of string // with removed white spaces let newGfg = gfg.trim();

// Old length console.log(gfg.length);

// New length console.log(newGfg.length)

`

**trimStart()

**trimStart() removes whitespace from the beginning of a string. The value of the string is not modified in any manner, including any whitespace present after the string.

JavaScript `

// Define a string variable let str = " Soul";

// Output the original value of the string console.log(str);

// Use the trimStart() method to remove leading whitespace from the string 'str' let part = str.trimStart();

// Output the resulting string after removing leading whitespace console.log(part);

`

**trimEnd()

trimEnd() removes white space from the end of a string. The value of the string is not modified in any manner, including any white-space present before the string.

JavaScript `

// Define a string variable let str = "Soul ";

// Output the original value of the string 'str' console.log(str);

// Use the trimEnd() method to remove trailing whitespace from the string 'str' let part = str.trimEnd();

// Output the resulting string after removing trailing whitespace console.log(part);

`

**padStart()

**padStart() pad a string with another string until it reaches the given length. The padding is applied from the left end of the string.

JavaScript `

// Define a string variable let stone = "Soul";

// Use the padStart() method to add padding characters "Mind " //to the beginning of the string 'stone' stone = stone.padStart(9, "Mind ");

// Output the resulting string after padding console.log(stone);

`

**padEnd()

**padEnd() pad a string with another string until it reaches the given length. The padding is applied from the right end of the string.

JavaScript `

// Define a string variable let stone = "Soul";

// Use the padEnd() method to add padding characters //" Power" to the end of the string 'stone' stone = stone.padEnd(10, " Power");

// Output the resulting string after padding console.log(stone);

`

**charAt()

**charAt() returns the character at the specified index. String in JavaScript has zero-based indexing.

JavaScript `

let gfg = 'GeeksforGeeks'; let geeks = 'GfG is the best platform to learn and\n'+ 'experience Computer Science.';

// Print the string as it is console.log(gfg);

console.log(geeks);

// As string index starts from zero // It will return first character of string console.log(gfg.charAt(0));

console.log(geeks.charAt(5));

`

Output

GeeksforGeeks GfG is the best platform to learn and experience Computer Science. G s

**charCodeAt()

**charCodeAt() returns a number that represents the **Unicode value of the character at the _specified index. This method accepts one argument.

JavaScript `

let gfg = 'GeeksforGeeks'; let geeks = 'GfG is the best platform\n
to learn and experience\n
Computer Science.';

// Return a number indicating Unicode // value of character at index 0 ('G') console.log(gfg.charCodeAt(0)); console.log(geeks.charCodeAt(5));

`

**split()

**split() splits the string into an array of sub-strings. This method returns an array. This method accepts a single parameter **character on which you want to split the string.

JavaScript `

let gfg = 'GFG ' let geeks = 'stands-for-GeeksforGeeks'

// Split string on '-'. console.log(geeks.split('-'))

`

Output

[ 'stands', 'for', 'GeeksforGeeks' ]

More JS String Methods

Below is the JavaScript string functions list:

Instance Methods Description
at() Find the character at the specified index.
anchor() Creates an anchor element that is used as a hypertext target.
charAt() Returns that character at the given index of the string.
charCodeAt() Returns a Unicode character set code unit of the character present at the index in the string.
codePointAt() Return a non-negative integer value i.e, the code point value of the specified element.
concat() Join two or more strings together in JavaScript.
endsWith() Whether the given string ends with the characters of the specified string or not.
includes() Returns true if the string contains the characters, otherwise, it returns false.
indexOf() Finds the index of the first occurrence of the argument string in the given string.
lastIndexOf() Finds the index of the last occurrence of the argument string in the given string.
localeCompare() Compare any two elements and returns a positive number
match() Search a string for a match against any regular expression.
matchAll() Return all the iterators matching the reference string against a regular expression.
normalize() Return a Unicode normalization form of a given input string.
padEnd() Pad a string with another string until it reaches the given length from rightend.
padStart() Pad a string with another string until it reaches the given length from leftend.
repeat() Build a new string containing a specified number of copies of the string.
replace() Replace a part of the given string with some another string or a regular expression
replaceAll() Returns a new string after replacing all the matches of a string with a specified string/regex.
search() Search for a match in between regular expressions and a given string object.
slice() Return a part or slice of the given input string.
split() Separate given string into substrings using a specified separator provided in the argument.
startsWith() Check whether the given string starts with the characters of the specified string or not.
substr() Returns the specified number of characters from the specified index from the given string.
substring() Return the part of the given string from the start index to the end index.
toLowerCase() Converts the entire string to lowercase.
toLocaleLowerCase() Returns the calling string value converted to a lowercase letter.
toLocaleUpperCase() Returns the calling string value converted to a uppercase letter.
toUpperCase() Converts the entire string to uppercase.
toString() Return the given string itself.
trim() Remove the white spaces from both ends of the given string.
trimEnd() Remove white space from the end of a string.
trimStart() Remove white space from the start of a string.
valueOf() Return the value of the given string.
stringSymbol.iterator This method is used to make String iterable. [@@iterator]() returns an iterator object which iterates over all code points of the String.
fromCharCode(n1, n2, ..., nX) This method is used to create a string from the given sequence of UTF-16 code units. This method returns a string, not a string object.
fromCodePoint(a1, a2, a3, ....) This method in JavaScript that is used to return a string or an element for the given sequence of code point values (ASCII value).
isWellFormed() This method is used to check if the string contains a lone surrogate or not
String.raw(str, ...sub) This is a static method that is used to get the raw string form of template literals. These strings do not process escape characters.
toWellFormed() This method is used to return where all lone surrogates of this string are replaced with the Unicode replacement character

Also Read

We have a complete list of Javascript string methods, to check those please go through this JavaScript String reference article.

Conclusion

JavaScript String is a collection of characters. They are one of the primitive data types. This tutorial covers these JavaScript string method that lets you manipulate strings.

JavaScript strings are a very important part of programming in JavaScript and one must know how to operate on them. Using these methods you will be able to perform numerous tasks involving strings.