For the upcoming methods, let’s take a string variable str with the value of “Welcome to MUO!” as an example.
let str = "Welcome to MUO!"
1. String.toLowerCase() and String.toUppperCase()
The toLowerCase() string method converts all the characters of the given string to the lowercase format, and similarly, the toUpperCase() method converts all the characters to the uppercase format. These functions don’t modify the original string.
Let’s check out these two methods with a quick example:
On running the above code on the console, you will receive the following output:
"welcome to muo!"
"WELCOME TO MUO!"
"Welcome to MUO!"
The concat() method is used to join two or more strings together. You can add one or more arguments to this method to concatenate them into a single string. It does not make any modification to the original string.
concat(str1, str2, str3, ...)
Here’s an example that showcases the concatenation of two strings to form a new string:
let str2 = " How are you?";
let newString = str.concat(str2);
"Welcome to MUO! How are you?"
3. String.indexOf() and String.lastIndexOf()
The indexOf() method helps you to find out the first index at which the specified character or substring is present. It begins from the left-hand side and traces the string to check if the given argument matches.
Let’s find out the index at which MUO is present in the string with an example:
If the given argument is not present in the string, the method returns a value of -1.
Similarly, the lastIndexOf() method returns the index of the last occurrence of the given character or string. Here’s an example:
Even though the alphabet e appears at index 1, the last occurrence of this character is at index 6 and hence is returned as the output.
The charAt() string method returns the character at the specified index in the string. It accepts only one argument, the index at which the character is to be retrieved. The index value ranges from 0 to length – 1.
Here’s an example of the charAt() method:
console.log(str.charAt(str.length - 1));
In the above example, when str.length – 1 was passed as the argument, the method returns the last character of the string. If you enter an invalid index that is beyond the permissible range, this method returns -1.
Similar to the charAt method, the charCodeAt() method returns the ASCII value of the character at the specified index. This string method takes only one argument, the index from which the character is to be retrieved.
str.charCodeAt(str.length - 1);
Once again, the index value ranges from 0 to length – 1 and if you try to pass an index beyond the permissible limit, this method will return -1.
As the name suggests, the replace() method helps you substitute one part of the string with another part. This method takes two arguments: the first one is the substring to be replaced, and the second one is the substring to be replaced with. This method does not make any modification to the original string.
For example, if you want to replace the word MUO with this website in the string variable, you can use the replace() method like this:
let newString = str.replace("MUO", "this website");
Welcome to this website!
Welcome to MUO!
The split() method is used to break down all the words or characters in a string as per the separator argument passed to the method. The return type of this method is an array. This array consists of all the characters or substrings, split according to the given separator. This method does not modify the original string.
For example, if a blank space (” “) is passed as the separator argument to the split method, this is how the output will look:
let splitArray = str.split(" ");
['Welcome', 'to', 'MUO!']
If you don’t pass an argument to the split() method, it’ll return an array with a single element consisting of your string variable’s value.
let splitArray = str.split();
['Welcome to MUO!']
The substring() method is used to obtain a substring or part of the original string. This method takes two parameters: the start index and the end index. The output substring starts from the specified start index and prints up to the end index – 1.
Here’s a quick example of the substring() method:
Note that the character at the end index is not a part of the output.
The search() method helps to find a particular substring or character inside the original string. This method accepts a group of characters or substring as an argument and traces through the string. Upon finding a match, the starting index of the matched portion is returned. Otherwise, this method returns -1.
You can make use of the search() method in this manner:
The trim() method removes all the white spaces in the string, before the first character and after the last character. This method does not require you to pass any parameters and doesn’t modify the original string. It’s extremely helpful for user input validation in forms.
Let’s take a new example to explore this string method:
let untrimmedString = " Welcome to MUO! ";
let trimmedString = untrimmedString.trim();
"Welcome to MUO!"
" Welcome to MUO! "