JavaScript String Methods that you should Know!

Published by Pawan Sargar on

JavaScript String Methods

JavaScript has some of the best string methods out there that can solve most of the common problems, without having to code algorithms and the complicated stuffs

Today, in this post we’ll discuss some of the string methods which will make you a better developer and also save your time and day.

So, let’s get started.

Strings methods we’ll look at today,

  • split()
  • slice()
  • toUpperCase()
  • toLowerCase()
  • replace()
  • indexOf()
  • trim()

#1 Split –

let split = "Game of Thrones".split(" ");
//['Game', 'of', 'Thrones']

JavaScript split() method, splits a string into it’s substring and returns a new array. As you can see in above example, we separated the words as an individual word into a new array.

Useful to count the number of words, or to separate the words.

#2 Slice –

let slice = "Joey Tribbiani".slice(0, 4);

JavaScript slice() method, returns elements with the given indexes, the two parameters refer to the start(0) and end(4). As you can see above, we’ve got the Joey, as the start is 0 index and end it before the 4th index.

This slice() method has much more use cases, you can also use this for Array. The best real-world example I can give is of the Twitter 240 characters limit, you can only get the desired amount of characters using this method.

#3 toUpperCase –

let upperCase = "tyrion lannister".toUpperCase();
//Tyrion Lannister

toUpperCase() method, just as the name suggests converts a string into the upper case or in the Capital letter.

This method is useful when getting data from the user, you can make certain words into upper case and get the data into your database.

This can also be done in CSS but it’s good practice to do in JS.

#4 ToLowerCase –

let lowerCase = "SQUID GAME".toLowerCase();
//squid game

toLowerCase() is just the opposite of the toUpperCase() method, which will convert upper case letters into the lower case.

I personally use this method mostly on the Nodejs side, when getting the parameters in the strings, I usually prefer to get it into the lower case to avoid errors.

#5 Replace –

let title = "Attack on Titan";
let replace = title.replace("Titan", "Cats");
//Attack on Titan

replace() method returns a new string with the replaced string. As you can see in the above example, we are replacing the Titan with Cats and returning a new string.

There are even more uses of replace() like, we can use Regular Expressions, and it doesn’t change the original string, so you can always have an original value.

#6 IndexOf –

let indexOf = "Android is better than Iphone".indexOf("Iphone");

indexOf() method returns the index or the positing of the given string or value. There are many use cases of this method like you can analyze the URL string or even you can check whether the value exists or not.

If there’s no value, it’ll return -1.

#7 Trim –

let trim = "  221B Baker Street".trim();
//221B Baker Street

trim() method cleans up the string by removing the empty white spaces and returns a new string. It’s best used when getting user data from the input, you don’t want to get the false white spacings from the user.

And, that’s it, sure there are many more methods, but these can be more helpful for your upcoming projects. Let me know if you think there are methods that we should’ve mentioned, we’d be glad to update our post.

If you liked this post make sure to share it with your friends and also make sure to follow our Coding Master newsletter.

Pawan Sargar
Follow him
Spread the love


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *