Sitemap
Sanket

Sanket

String Methods in JavaScript

1 min readMay 20, 2024

Strings are sequence of characters , that are used to store text. They are denoted by single and double quotes.

Get Sanket’s stories in your inbox

Join Medium for free to get updates from this writer.

The following are some string methods provided by JavaScript :

  1. charAt( ) : It takes a index as argument and returns character present at that index.
let str = "Iron";
console.log(charAt(1)); // gives -> 'r'

2. toLowerCase( ) : Takes string as argument an returns the string converted to lower case.

let str = "IRON MAN";
console.log(str.toLowerCase()); // "iron man"

3. toUpperCase( ) : Take a string as argument and retrurn the string in upper case.

let str = " iron man";
console.log(str.toUpperCase()); // "IRON MAN"

4. replace( ) : The method returns a new string with one, some, or all matches of a pattern.

let str = "Rohan is going back home.";
console.log(str.replace("Rohan","He")); // "He is going back home."

5. substring( ) : It takes two arguments , first start index and second end index and returns back string with that index range.

let str = "Sweet Home";
console.log(str.substring(0,6)); // "Sweet"

6. trim( ) : It removes the whitespaces from the start and end of string.

let email = "   tony@4gmail.com   ";
console.log(email.trim()); // "tony@4gmail.com"

7. split( ) : It takes a pattern and divides the string on basis of that and returns back array of substring.

let str = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";
let months = str.split(",");
console.log(months); // ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

8. indexOf( ) : It takes sequence and returns first index from which it starts.

let str = "Iron Man";
console.log(str.indexOf('I')); // 0

--

--