JavaScript string methods

 JavaScript string methods

JavaScript has some amazing methods to work with strings. We call these string methods. Let's understand a few of them.



Suppose the string is "We love doers!";

let str = "We love doers!";

You can count the number of characters in the string by using the length method. The output would be 14 in our case. Go ahead practice it on your own string.

str.length;

You can search for something in a string by using the search method. It returns -1 if nothing is found. We can look at the practical usage as we progress. It will return 3 as the word love begins at the third character. Even the spaces are counted.

str.search("love");

Check first occurrence of some word using indexOf method. Returns 8 in our case.
str = "We love doers!" doers.";
str.indexOf("doers");

Suppose you wanted to check the last occurrence of some word, you could use locate method. Returns 15 in our case. It returns -1 if it's not found.

str.lastIndexOf("doers")

To convert the string to uppercase use the toUpperCase method. It will capitalize everything.
str.toUpperCase(str);

There are more of these covering them in entirety is a mess. To start of when you get comfortable with these check out some of the other ones on Internet or W3Schools. 

As always video lesson will be added soon. Remind me to delete this line. :)

Post a Comment

0 Comments

React