JS String Related Functions
Tags: javascript, string, programming, tutorial
Published: 2021-12-20 17:50:47

In JS, String is one type of variable. String stores a series of characters (A-Z, a-z, 0-1, space, special characters etc.) You can use double quote/ single quote to store a string variable. It can be declared with let, const and also using var. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dn3bwrjew673197cgiw5.JPG)

String indexing starts from 0 and so on. 

**String Methods:**

**charAt():**
This function returns the character from the string.of the specified position which is given as a parameter.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05geq1he9e83070ua29n.JPG)

**concat():**
This joins more than one function together. The resulting string doesn’t change any of the existing string after concatenation. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovurj2a9f9g3vpfqxn39.JPG)

But there is a more easy method than using which you can easily handles necessary spaces. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8egi6dird5wlxj1n546p.JPG)

**includes():**
This returns true if the string to be checked contains the specific string which is sent as a parameter. Otherwise, false. It is case sensitive. So, the output of this function is boolean. The position from where the start operation will start is optional, default 0.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oiqgwui8ts1ql6mzdyp9.JPG)

**endsWith():**
This function returns true if a string ends with the specified string which is sent as a parameter. Otherwise, false. It also gives output a boolean. Also case sensitive.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52n0ewztqyfioa5p343r.JPG)

**indexOf():**
This method is also case sensitive. It checks if the specific value is in the string or not. It it is, then returns the position of first occurrence of it, otherwise returns -1. It is optional to give the information as a function parameter from where the value checking will start. By default, it starts from 0 position.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azwoa6rkyhvub239a6k2.JPG)

**replace():**
This replaces the specific part of the string string with any specific value. It doesn’t affect the original string.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jhhzyoml2cqhjofuqde.JPG)

**slice():**
This method extracts a part of the string and returns that part as output. It seeks 2 parameters, start & end. end is optional.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltfnhc9z7o41nr8wew4w.JPG)

split():
This method splits the target string and returns the substrings according to the condition of splitting. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7k2ex4fj6834sexlz5jw.JPG)

**startsWith():**
This gives output true if the specified string starts with a specific string. Otherwise false. It also takes an optional parameter which defines the starting position.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67pl15det9j99crsrhvo.JPG)

**substr():**
It also returns a part of the string like slice(). 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8frlupx3af61qhjvgs3.JPG)

But they have a couple of differences between them.  With a negative argument, slice() starts counting from the end of the string, whereas substr() considers negative value as zero. 

**toLowercase():**
This converts the whole string to a lowercase string.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87dhlwhjyql1vkgqyppz.JPG)

**toUppercase():**
Converts a string to uppercase letters.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92ge6mztjn1vnlaonqfd.JPG)

**trim():**
It removes unnecessary white spaces from both sides of the string.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypz4kesgjzp3l3gpfzqe.JPG)

**trimStart() + trimEnd():**
trimStart() removes whitespaces from the starting of the string. rimEnd() removes whitespaces from the end of the string.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2ezmtwul88ay8gsolme.JPG)