Some popular string Methods in JavaScript
Tags: javascript, webdev, beginners, tutorial
Published: 2021-11-08 16:14:13

## What is a String?
- The sequence of one or more characters enclosed within quotation marks is called a string.
- The quotation can be single quotes `''` or double quotes `" "` or backtick ``.
- And, the sequence of characters can be alphabets, numbers, symbols, etc.

**Example of creating strings**
![Create string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16zmb733im0k1vrmlp8f.png)

## Some commonly used JavaScript methods:
1. **Length**

- As the name suggests, `length` returns the length of the string.
![Length](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghmt0kg9k6wexemge3ga.png)

- Note, in the above example the whitespace, comma, and exclamation mark are also part of the string.


2.**charAt(index)**

- The `charAt()` returns the character at a specified index in a string.
- The very first character of the string has an index of 0, the second character has index 1, and so on...

![charAt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/190dnlhpjko7z6bygbq8.png)
 


3.**substring(start, end)**
- This method extracts the part of the string between `start` and `end` and returns the substring.

![substring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vuuqvhiqod6i73slryw.png)
 
4.**substr(start, length)**
- The `substr()` method returns the specified number of characters from the specified index (`start` parameter) from a given string.
- Here, `start` defines the starting index from where the substring is to be extracted from the original string.
- And, `length` defines the number of characters to be extracted from the specified start index.
- **Note:** If the `length` parameter isn't given, then all the characters from start till the end of the string are extracted.

![substr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zm8ojwj9bmh3ez7gwh4q.png)

5.**concat()**
- The `concat()` method joins two or more strings.
- The `concat()` method doesn't modify the  original strings, but it returns a new string.
![concat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kam7ly6liq3z4g9wzkn4.png)
 

6.**toUpperCase()**
- The `toUpperCase()` method converts the strings to upper case letters.
![toUpperCase()](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opks9yhun2u40ne4y6v4.png)


7.**toLowerCase()**
- The `toLowerCase()` method converts the strings to lower case letters. 
![lower case](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ih7jxub77z1lwzw63dv.png)
 


8.**slice(start, end)**

- The `slice()` method extracts and returns a part of the string from start to (excluding) the end character.
- If there's no second argument specified, then the slice method extracts till the end of the string.

![slice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4gjbvt3fnqnfqs6tfpz.png)

- `slice()` also works on negative indices. If a negative index is specified, the string is extracted from the right end.
- The negative index starts from -1 and it indicates the last character of the string, -2 is the second last character, and so on...
![slice](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwnkwsphk79umaa8t7ez.png)

9.**replace(substring)**

- The `replace()` method is used to replace a part of a given string with a new substring.
- One important thing to note here is that the `replace()` method does not change the original string on which it is called upon. It simply returns a new string.

![replace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cccidljk1o9yc3ecz596.png)

10.**includes(substring)** 
- The `includes()` method does a case-sensitive search on the original string to see if the specified substring is present in the string or not.
- If the specified string is present, the method returns `true` otherwise `false`.
![includes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdn7mzevxtdk4nipdy7k.png)

11.**trim()**
- The `trim()` method removes leading and trailing whitespaces from the given string.
![trim](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phzu5o545e5200jq06hb.png)

## Conclusion
- That is all from my side. I hope this article provides you with the basics of some popular string methods used in JavaScript.
- Working through the examples is the best idea to understand these methods. Play with the examples.

### Keep Learning!!