This is a method that will return the number of characters in a string. Here's an example:
const animal = "gorilla"; console.log(animal.length); // logs 7
The .length method checked the animal string and returned its length. Then, the number 7 was logged to the console. This represents the total number of characters in the string.
This method tells you the location (index) of the first time specified characters appear in a string. Here's an example:
const color = "light blue"; console.log(color.indexOf("blue")); // returns 6
This method is different from .length because .length returns the total character count, whereas .indexOf returns the index of the first character of the part of the string you're looking for. The indexes in a string start at 0. If the .indexOf method does not find the specified characters in a string, it will return -1.
This method gives you the location (index) of the last time specified characters appear in a string. The number that is returned is the location of the first character of the specified characters which the method is searching for.
const shark = "My favorite shark is the Great White Shark"; console.log(shark.lastIndexOf("shark")); // returns 12
Notice the lastIndexOf("shark") is case sensitive. Therefore, it returns the index of 12 because that is last time the lowercase version of "shark" appears in the sentence. Also, notice that it returns the index of the first character of that word.
This method takes strings and returns the part you slice out of it. The method takes a start point and an end point (indexes). To use this method, you have to know where the substring you are interested in starts. Here’s an example:
let superheroes = "Superman"; console.log(superheroes.slice(1 , 3)); //up console.log(superheroes); //Superman
If you run this code, you will see that when you slice from index 1 to index 3 in the array, it just makes a copy of that part of the array without changing the original string. You can see this by running console.log() on the original string after slicing. If you only provide one parameter, the remainder of the string will be sliced. If you provide a negative parameter, the slice will start from the end of the string and work backwards.
According to MDN Web Docs "The substring() and slice() methods are almost identical, but there are a couple of subtle differences between the two." Here are the differences:
- With the substring method, if the first index is greater than the second index, the arguments are automatically swapped. The slice method will return an empty string.
- The substring method automatically treats arguments that are negative or NAN as being the number 0. Whereas, with the slice method, negative values are counted backwards from the end of the string. Here's an example:
let movie = "Soul"; console.log(movie.slice(0, -1)); // Sou console.log(movie.substring(0, -1)); // becomes (0, 0)
This method is very simple. It replaces a group of characters in a string and substitutes another. Here's an example:
let favoriteColor = "My favorite color is green"; favoriteColor.replace("green", "blue");
A string is converted to upper case with toUpperCase() and a string is converted to lower case with toLowerCase(). Here is an example of both of these methods:
let alphabet = "a, B, c, D, e, F, g, H, i, J, k, L, m"; console.log(alphabet.toUpperCase());// all uppercase console.log(alphabet.toLowerCase());// all lowercase
concat() joins strings together. Here is an example:
let brand = "Microsoft"; let system = " Windows"; let operatingSystem = brand.concat(system); console.log(operatingSystem);
This method is another way to accomplish string concatenation which we learned about in a previous post.