DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for πŸ“š String methods in JavaScript - Part 1
Ekaterine Mitagvaria
Ekaterine Mitagvaria

Posted on

πŸ“š String methods in JavaScript - Part 1

JavaScript comes with a variety of built-in methods that are ready to be used as soon as the language is implemented. These methods are pre-written and optimized code that can perform specific tasks such as mathematical operations, string manipulation, and even manipulating the Document Object Model (DOM).

Built-in methods in JavaScript are used to perform repetitive tasks more efficiently and to make the code more readable. They also provide a consistent and reliable method for performing specific actions.

The history of built-in methods in JavaScript dates back to the release of the language in 1995. Since then, the number and functionality of built-in methods have grown significantly, with the addition of new methods in each version of the language.

Methods can be used with Objects, Arrays, and Strings and they offer the possibility to work with data in an efficient way without having to write repetitive functions over and over. Sounds awesome, right?

Let’s discover Array built-in methods and understand what they do however remember one important thing! Some methods return a new string and some mutate the existing one - it’s a very important thing to remember

String.prototype.at()

String.prototype.at()

This method helps to find a single character at the index we specify and return a character it found. You can write positive as well as negative values. Negative values just count back from the last character.
If you want to find the first character in the string, the index should be 0, if you want to find the last character index should be -1.
If nothing is found, it returns undefined.
This method creates a new string so make sure to save it in a variable.

String.prototype.charAt()

String.prototype.charAt()

This method does exactly the same as the previous method we have mentioned however there are differences.
If you do not provide any index or an index that cannot be converted to an integer, it will use the default value of the index which is 0, so it will return the first character.
Instead of writing -1 for the last character you need to write string.length - 1.
If nothing is found, it will return an empty string instead of undefined.
This method also creates a new string so make sure to save it in a variable.

String.prototype.charCodeAt()

String.prototype.charCodeAt()

This method returns a number from 0 to 65535 and this number is the range of UTF-16 for the character - every character has its numeric value which represents this character. You also use an index number here, which will find the character but return the Unicode this time.
If the index is longer than the string length or less than 0, it returns NaN (Not a Number).
It also sets the index default to 0 if the index is not indicated.
This method creates a new string so save it in a variable.

String.prototype.codePointAt()

String.prototype.codePointAt()

This is exactly the same as charCodeAt() however the main difference is the code it returns. Besides UTF-16 we can also have Unicodes. For example, some emojis or characters consist of so-called β€œSurrogate pairs”. It means that they consist of more than 1 character. That’s why it’s a more modern way to find a Unicode for the characters as the newer characters, like emojis, for example, might contain more than one character and will not fit inside the range of UTF-16.

String.prototype.normalize()

String.prototype.normalize()

As we have already discussed Unicode, this will be easier to understand. What normalize does is that it converts Unicode back to a normal string, normalized it. However, the usage of this method can be a situation when someone needs to compare Unicode which is different however when you convert them to string they are actually the same. The reason this happens is that the same strings can be created with slightly different characters (Unicode).
You can skip the argument and if you do it will change to a string by using the β€œNFC” Unicode normalization form. There are also different ones that you can optionally add like β€œNFD”, β€œNFKC” and so on.

String.prototype.concat()

String.prototype.concat()

This method links two different strings to each other and creates one single string from them.
We add two arguments. First, we add the desired separator, which should be between these strings. If you want an empty space between strings you simply write an empty space inside the string, if you need a comma, you write a comma inside the string. Then, we add the second string we want to link with the first one.
You can write 3rd or 4th arguments with an additional string you would like to add and it will be added in the order you have written them. What is the limit of the arguments you can write? I am not sure but I doubt you will need a million arguments :))
If any of the strings are not strings they will be converted to a string before concatenating.
This method creates a new string so make sure to save it in a variable.

String.prototype.endsWith()

String.prototype.endsWith()

Returns true if it finds the string at the end of the string and false if not. This method receives two arguments where the second one is optional:


  1. First argument is the string we are looking for.
  2. Second one is the end positions where the string can be found however the number you write needs to be the last character index + 1. Note that the strings are case-sensitive so β€œString” and β€œstring” are not equal.

String.prototype.includes()

String.prototype.includes()

It’s very similar to the previous method however it searches for a string not just at the end but across the whole string and returns true as false. It also has a second argument however the difference is that instead of indicating the last index position you indicate the start - the index where the string starts.
Note that the strings are also case-sensitive so β€œString” and β€œstring” are not equal.

String.prototype.indexOf()

String.prototype.indexOf()

This method is also very similar to the previous method includes() however instead of true or false it returns the index where the target string was found for the first time. Of course, the string doesn’t last just one index, so it will return the very first index it starts appearing at, not all the numbers. If the string is repeated several times it will return the index of the one that appeared for the first time.
The method also accepts a second argument where you specify at one index you want to start searching.
If nothing is found it returns -1.
Note that the strings are also case-sensitive so β€œString” and β€œstring” are not equal.

String.prototype.lastIndexOf()

String.prototype.lastIndexOf()

This method is exactly the same as the previous one however instead of returning the first occurrence of the string, it returns the last occurrence, as the name suggests. The same applies to the second argument we can use, where we indicate the index of the last occurrence. The rest is just as same as in the indexOf() method.

There are plenty of other interesting string methods to learn and I am going to cover them in the next part!

πŸ“Œ Enjoyed the post? Please let me know in the comment down below!

Top comments (0)

Classic DEV Post:

caching

Web Caching Explained by Buying Milk at the Supermarket