10 Useful Javascript String Functions and how to use them?

Tanvir sazib
3 min readMay 5, 2021

A string is a data form similar to an integer and a floating-point unit that is used to represent text rather than numbers in programming. It is made up of a series of characters that may include spaces and numbers. The terms “hamburger” and “I ate 3 hamburgers,” for example, are both strings. If listed correctly, even “12345” could be considered a string.

In JavaScript String object is used to represent and manipulate a sequence of characters.

How to create a string in JavaScript:

A JavaScript string is zero or more characters written inside quotes.

const stringOne = "Hey I am a string";
const stringTwo = "Hey I am second string";

String Functions:

1. stringName.charAt():

The charAt() method returns the character at the specified index in a string.

const stringOne = "Hey I am a string";
//expected output: y

Here, the given index is 2 between the charAt(). So, here output shows at index 2 is ‘y’.

2. stringName.concat():

The concat() method is used to join two or more strings.

const stringOne = "Hey I am a string";
const stringTwo = "Hey I am second string";
console.log(stringOne.concat(" ", stringTwo));//expected output: Hey I am a string Hey I am second string

This method does not modify the current strings; instead, it returns a new string containing the combined text.

3. stringName.indexOf():

The indexOf() method returns the location in a string of the first occurrence of a given value.

const stringOne = "Hey I am a string";
//expected output: 11

If the value to look for is never found, this method returns -1.

4. stringName.replace():

The replace() method looks for a specific value or a regular expression in a string and returns a new string with the given values replaced.

const stringOne = "Hey I am a string";
console.log(stringOne.replace("string", "replaced string"));
//expected output: Hey I am a replaced string

5. stringName.slice():

The Slice() function takes pieces of a string and returns them as a new string.

const stringOne = "Hey I am a string";
console.log(stringOne.slice(3, 8));
//expected output: I am

To define which part of the string you want to remove, use the start and end parameters inside the slice separated with a comma.

6. stringName.split():

The split() method returns a new array after splitting a string into an array of substrings.

const stringOne = "Hey I am a string";
console.log(stringOne.split(" "));
//expected output:[ 'Hey', 'I', 'am', 'a', 'string' ]

Here the string was split by a single white space. So whenever it found a white space it split the string into pieces.

7. stringName.substr():

The substr() method extracts sections of a string starting at the specified character and returning the specified number of characters.

const stringOne = "Hey I am a string";
console.log(stringOne.substr(0, 8));
//expected output: Hey I am

Here, the starting index was 0, and the ending index was 8. So, the function made a substring of the main string from the index of 0–8.

8. stringName.toLowercase():

The toLowerCase() function converts a string to lowercase letters.

const stringOne = "Hey I am a string";
//expected output: hey i am a string

9. stringName.toUppercase():

The toUpperCase() function converts a string to uppercase letters.

const stringOne = "Hey I am a string";
//expected output: HEY I AM A STRING

10. stringName.match():

The match() function looks for a match in a string against a regular expression and returns the results as an Array object.

const stringOne = "Hey I am a string";
//expected output:[ 'am', index: 6, input: 'Hey I am a string']

Here, the match function searches for ‘am’ in the string, and got it at index 6. So, it returns a result as an Array.