10 Javascript strings you need to know

Aktaruzzamanjoti
2 min readMay 5, 2021

Are you want to start with javascript strings? Ok. You are on the right track. Now let’s talk about some magical javascript strings. After knowing about those strings, I’ll sure you would fall love in with javascript more than before.

1. concat()

this method concatenates two strings and returns a new string. Have you ever heard about it whether a string can concatenate with another? It’s been really exciting method you should know about it.

const str1 = ‘Aktaruzzaman’;
const str2 = ‘Joti’;

console.log(str1.concat(‘ ‘, str2));
output: “Aktaruzzaman Joti”

2. slice()

if you want to be a javascript developer, this method can be used several times. the purpose of this method is to extract a section and returns a new string without modifying original string.

const str = ‘I want to be a world-class web developer’;

console.log(str.slice(14, 25));
output: “world-class”

3. indexOf()

this method determines the index of the specified string. In Javascript, index of the first string is 0.

const paragraph = ‘I am Aktaruzzaman Joti’;

const searchTerm= ‘Joti’;
console.log(paragraph.indexOf(searchTerm))

output: 3

4. trim()

Another fun method it is. It is reffered to removes whitespace from both sides of a string. Example below:

const greeting = ‘ Aktaruzzaman Joti ‘;

console.log(greeting.trim());
output: “Hello world!”;

5. split()

You can separate a string by using this method mentioning it’s limit. Now let’s see how it works.

const str = ‘I love my country.’;

const words = str.split(‘ ‘);
console.log(words[3]);

output: ‘country’

6. substr()

This method has two parameters. One is to start a portion of a string and another parameter executes the length of this string.

const str = ‘Bangladesh’;

console.log(str.substr(1, 2));
output: “an”

7. replace()

This method replaces a string with your given string. It’s a simple way to execute.

const p = ‘I love my country’;

console.log(p.replace(‘love’, ‘lik’));

output: ‘I like my country’

8. startsWith()

To check whether a string starts with a word you referred to, returning true or false.

const str1 = ‘Programming Hero’;

console.log(str1.startsWith(‘Programming’));
output: true

9. inclueds()

In this method, you can be checked whether a string may be found within another string. It returns true or false. Let’s look at the code:

const str1 = ‘ I am a student of Programming Hero’;

console.log(str1.includes(‘strudent’));
output: true

10. endsWith()

You can search a string whether it exists in another string or not. It also returns true or false value. Look at a glance:

const str1 = ‘Cats are the best!’;

console.log(str1.endsWith(‘best’, 17));

output: true

How do you feel right now? I sure, you might gain confidence better than before. So, keep practicing and keep your footwork in developer’s world! All the best for you!

--

--

Aktaruzzamanjoti

I'm a curious Web developer who is hungry to know about development