10 Methods of String in JavaScript

1. toUpperCase():

This method is used to convert the letters of a string to uppercase i.e. capital letters without changing the value of the string.

Example of toUpperCase() method of JavaScript
Example of toUpperCase() method

2. toLowerCase():

This method is used to convert the letters of a string to lowercase i.e. small letters without changing the value of the string.

Example of toLowerCase() method

3. charAt():

This method is used to get the character present in given index number of a string.

This method takes a parameter i.e. index number. The parameter should be an integer value between 0 and (length of string -1) inclusive.

If no index is provided, by default the method will take 0 as its parameter. If index number is greater than or equal to string length then it will return empty string.

Example of charAt() method

4. concat():

The name of the method is short form of concatenate which means linking together. This method is use to join the string.

Example of concat() method

5. includes():

This method is used to find a string inside a string. It returns only Boolean value i.e. true if searching string is present in main string else false. This method is case sensitive.

It takes two parameters.

  1. the string you want to search
  2. from what index number you want to start the search.

Note: Second parameter is optional. If not provided it will take a default value 0.

Example of includes() method

6. indexOf():

This method is used to find the index of a string inside a string. It will return the value of the index number of the first match. If there is no match it will return -1. This method is case sensitive.

This method takes two parameters:

  1. string you want to find index number of
  2. from what index number you want to start the search

Note: Second parameter is optional. If not provided it will take a default value 0.

Example of indexOf() method

7. slice():

This method is used to extract a string from a string without committing any changes to original string.

This method takes two parameters:

  1. Start index number
  2. End index number. If not provided the method will extract from start to the last part of the string.

Note: The letter in end index number is not included in the extracted string.

In this method you can also use negative number in order to pick from the last. The index of last character is considered as -1.

Example of slice() method

8. substring():

This method is used to extract required part of the string by using a range of index number as parameter. The extraction starts from start index and ends before the end index ( doesn’t include end index). If end index is not provided the extraction occurs up to the last character.

If no parameter is given it will return the original string.

If provided start index is greater than end index, the method consider end index as the start index and start index as end i.e. the value of start and end index interchanges.

Note: Original string remains unchanged.

Example of substring() method

9. split():

This method splits a string and return an array containing all of the split part in orderly manner.

It takes a string as a parameter which is used to split the original string. Returned array will not contain the parameter.

If an empty string is used as a parameter, the sentence will be split into individual character and will return as array.

This method also takes an optional parameter which is the length of returning array.

Example of split() method.

10. startsWith():

This method is used to determine whether a string starts with given character or string. This method is case sensitive and only returns Boolean value.

It takes two parameter:

  1. String you want to search
  2. Index number, from where you want to start your search.

Second parameter is optional, if not provided it will take a default value 0.

Example of startsWith() method

Note: This article is the summary, of some method, of what I understand while reading about String in MDN.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Create simple animated bar chart in React

JS String Part 2

What is Axios and how to use it with React 🚀

How to start learning Angular? Discover the basic concepts

How to start learning Angular?

CORS policy Error In Working With Graphql, ReactJs,Apollo client.

How to Learn React #11: The Simple Formula to Understand React Form

Welcome Article

Use Grammarly in Overleaf

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
AL- RAFSAN AMIN

AL- RAFSAN AMIN

More from Medium

Understanding Array.reduce(), JavaScript

JavaScript Closures for beginners

Switch overview in JavaScript

IIFE(Immediately Invoked Function Expression) JavaScript