JavaScript String Operations

Okan Davut
3 min readAug 5, 2021

Hi everyone, in this article, I will try to explain string methods in JavaScript. I know you can find too many articles or videos about this topic. But I am taking notes for myself and sharing them :)

Let’s start with the first one.

Length

Length is the method that you can get the string letter count. You can use the length for all strings. For example for “Okan”.length returns 4. You can see the example below.

Slice

You can seperate strings using slice. Pass the parameters first one is start point of seperating. Second one is ending point of seperating. Please check below example.

slice(start, end)

Substr

Substr seems same like the slice. But for the substr you give starting point and length for separating to string items.

The important difference is if you know the index point for stop to slicing you can use slice. But if you know length of stopping point so you need to use substr.

substr(from,length)

Replace

As you can understand from define ‘replace ’method is used for find and change the characters with others.You can pass one or more charachters to finding or changing. You can see the below example.

replace(searchValue, replaceValue)

ToUpperCase and ToLowerCase

ToUpperCase is the method that you can make all letters to upper case.You can see the below example.

ToLowerCase is the method that you can make all letters to lower case. You can see the below example.

Both methods are so simple and easy to use.

Concat

You can join the string items using concat. As you can see method not gets just 2 strings. You can pass strings to method how much you want. You can see the below example.

concat (…strings)

Trim

If you want to delete all empty characters in the end of string you need use trim method for this action. You can see the below example.

CharAt

If you want to get one character from value using the index, you can use charAt method. In the below example i try to get ’n’ character so called with 3 index number.

CharCodeAt

Actually charCodeAt method works same like charAt. Its gettings indeks of the character that you want. But it returns char code of that value.

With this article i just wanted to take a note for myself about JavaScript string operations. I hope it’s useful for you.

Thank you for reading blogpost!

--

--