JavaScript String Methods

23 Apr 2025 | 8 min read

There are several built-in methods available in JavaScript for string manipulation. Strings are important for taking input from the end user, string formatting and data processing. There are some built-in methods in JavaScript for accessing, altering, and analysing the string. We will go through some string Methods and their usage with examples in this article.

What is a String?

A string is a sequence of characters in JavaScript and it could be enclosed between either single (') or double (") or backticks (`). In simple understanding, string is a data type utlized to store and manipulate text in a JavaScript program. They are immutable, which means that there is no way to change their content directly but there are many ways to work with it. A string is a type of data that can be composed of letters, digits, symbols, and spaces. JavaScript handles strings as objects, so it provides us with built-in methods to extract, find, replace, and manipulate the text as quickly as possible.

String length in JavaScript

The length of a string includes spaces and special characters. It is helpful for user input validation, string size checking, or doing something based on the length of string.

Output:

 Length: 18 Length: 0 Length: 27 

String charAt() in JavaScript

The charAt() returns a character at a given index of a string. The index starts from 0 and returns an empty string when the index object is out of range.

Output:

 Character at index 4: n Character at index 0: T Last character: h Character at index 20: 

String charCodeAt() in JavaScript

The charCodeAt() returns the Unicode value (ASCII code) of the character at the specified index of a string. This comes in handy when working with character encodings or conversions.

Output:

 ASCII code at index 1: 112 ASCII code at index 0: 84 ASCII code at last index: 104 

String at() in JavaScript

The at() method represents characters by index, and it supports negative indexing to get characters from the tail of the string. This allows for easier access from the end of the character compared to regular indexing.

Example

Output:

 Character at index -1: h Character at index -5: t Character at index 3: i Character at index 10: undefined 

String slice() in JavaScript

The slice() method is used to get the part of a string and returns the part in the form of a new string. The function takes two arguments which are the beginning and ending indexes. Omitting the end index makes the slice go all the way to the end of the string.

Output:

 Slice from 0 to 10: JavaScript Slice from 11 to end: Programming Last 5 characters: mming Slice excluding last 10 characters: JavaScript P 

String substring() in JavaScript

It returns a portion of the string based on beginning and ending indexes. It does not take negative indexes unlike slice(). The method automatically swaps the start index against the end index if the start index > end index.

Output:

 Substring from 0 to 4: Tpoi Substring from 4 to 0 (auto swap): Tpoi Substring from 4 to end: ntTech 

String substr() in JavaScript

The substr() method returns the characters in a string starting at a specified location through to the specified number of characters. This method is now deprecated instead of this slice() is used.

Output:

 Substr from 4, length 6: ntTech Substr from 0, length 4: Tpoi Substr from -6, length 6: ntTech 

String toUpperCase() in JavaScript

The toUpperCase method is used to convert all the characters of a string to uppercase. This is helpful for normalizing text input or use in case insensitive comparisons.

Output:

 Uppercase: HELLO Uppercase: TPOINTTECH USERS 

String toLowerCase() in JavaScript

The tolowerCase() method is used to return the value of a string converted to lowercase which is useful for case-insensitive searches or comparisons.

Output:

 Lowercase: hello Lowercase: javascript 

String concat() in JavaScript

This method is basically used to join two or more strings together. Although using concat() produces the same result as the + operator, it can sometimes make code clear when communicating complex expressions.

Output:

 Concatenated string: Hello TpointTech Concatenated with multiple strings: Hello TpointTech Users Concatenating empty string: Hello 

String trim() in JavaScript

This method removes whitespace from both sides of a string. It's handy in scenarios where user input needs to be cleaned off before passing to storage like a database or for further processing of the data.

Output:

 Trimmed string: Hello World Trimmed string: JavaScript 

String trimStart() in JavaScript

The trimStart() method is exactly like removing whitespace from the start of a string. It is useful when you have indentations or leading spaces.

Output:

 Trimmed start: Hello World Trimmed start: Trim me 

String trimEnd() in JavaScript

This trimEnd() method removes whitespace from the end of a string.

Output:

 Trimmed end: Hello World Trimmed end: Remove spaces at the end 

String padStart() in JavaScript

The padStart() method adds a character to the start of a string until the string reaches a specified length. We can use this during number formatting, text alignment while printing output, etc.

Output:

 Padded start: 0005 Padded start: ****Hi 

String padEnd() in JavaScript

The padEnd() method pads the end of a string with a specified character until it reaches a certain length.

Output:

 Padded end: 5000 Padded end: Hello!!!!! 

String repeat() in JavaScript

The repeat() method returns a new string of a given number of copies of an original string. This is handy when we want to have repeated patterns in strings.

Output:

 Repeated string: Hello Hello Hello Repeated stars: ********** 

String replace() in JavaScript

The replace() method replaces one value with another in a string. Unless using a global regular expression, it only replaces the first match.

Output:

 Replaced string: Hello JavaScript Replaced color: The sky is red. 

String replaceAll() in JavaScript

String class also has a replaceAll() method which replaces all occurrences of a given value with another value hence it is suitable for mass text replacement.

Example

Output:

 Replaced all occurrences: Hello JavaScript, JavaScript! Replace all 'abc': xyz xyz xyz 

String split() in JavaScript

The split() method divides a string into an array using a separator string and it is an often-used method for parsing comma-separated values or processing input strings.

Output:

 Split string: [ 'Hello', 'World', 'JavaScript' ] Split by spaces: [ 'This', 'is', 'a', 'test', 'sentence' ] 

String indexOf() in JavaScript

The indexOf() returns the position of the first occurrence of a specified value in a string. If the value is not found then it returns -1. This method is used for checking whether a text contains a specified substring.

Output:

 Index of 'TpointTech': 7 Index of 'Welcomes': 15 Index of 'Yashraj': 6 

String lastIndexOf() in JavaScript

The lastIndexOf method returns the last index of a specified value in a string. If the value is not found then it gives -1. It is helpful when you want to look for words that have been recently repeated.

Output:

 Last index of 'JavaScript': 18 Last index of 'west': 34 Last index of 'Python': -1 

String search() in JavaScript

The search() method is utilized to search a string for a specified match and returns the index number of the first match. If no match is found then it returns -1.

Output:

 Search for 'JavaScript': 7 Search for 'rain': 4 Search for 'hello' (case-sensitive): -1 Search for 'hello' (case-insensitive): 0 

String valueOf() in JavaScript

The valueOf() method is utilized to return the primitive value of a string. It is the default JavaScript string method.

Output:

 Tpoint Tech 

Conclusion

JavaScript String methods are so powerful methods for text processing. We use them especially when we want to efficiently process strings (like processing text in form validation, text parsing, data formatting, etc.) for various programming scenarios.