Strings and Its Cousins(Methods) | JavaScript…

Nayeem Uddin
2 min readNov 2, 2020

When we start to learn a programming language, we all get stuck in so many things. I was stuck in several why’s!!! So I decided to break something here…

What is String?

const sentence =Hey I’m String;

you can to a lot with this string. So here are some of their cousins…
charAt, concat, includes, endsWith, indexOf, lastIndexOf, replace, slice, split, startsWith, substr, toLowercase, toUppercase, trim, trimStart, trimEnd

Yes, there are more but these are some which I will try to explain…

How you will use them?

String.cousins… that simple :)

  1. CharAt
    This will give you the index member!!

console.log(`Who is in the index 5?? here is the answers ${sentence.charAt(5)}`) // Ans : I

So, this will give the value lives in the string…

2. Concat

This is just like a bridge between two Strings…

const sentenceOne = One;

const sentenceTwo = Two;

console.log(`sentenceOne.concat(‘LOVES’, sentenceTwo)`) // One LOVES Two.

Here concat just gave you one new string from two.

3.Include

This is just like an elder cousin who keeps his eye on you…

const sentence = “ He stole Bags, Keys, and Mobile”

//I lost my Keys, lets find With- include

const objectOne = Keys;

console.log(‘ Does he have my ${objectOne}?… ${sentence.includes(objectOne) ? “yes” : “No” }) // ans yes

It means you can check if the variable is in there or not.

4.endWith

This is just the elder cousin but this only takecare of the last part…

const sentence = “Hello!”

console.log(sentence.endsWith(“!”)); // true

console.log(sentence.endsWith(“?”)); // false

It will always find the last portion in the String…

5. indexOf

now this will give you index number(The possition of the word).

const sentence = “Find My Possition”

console.log(sentence.indexOf(“P”)); // ans: 8

6.lastIndexOf

This one will give you the last index
const sentence = ‘find me Yo’;

const searchTerm = ‘o’;

console.log(`${sentence.lastIndexOf(searchTerm)}`); // ans 9

7.replace

This has a supper power to change something with something…

const sentence = ‘Hello yo mo’;

console.log(`${sentence.replace(‘mo’,’yo’)}`); // ans : hello yo yo

I told replace to change “mo” to “yo”..

8.toUpperCase

This will make words to capital words…
const sentence = ‘hEllo yo mo’;

console.log(`${sentence.toUpperCase()}`); //ans: HELLO YO MO

If there is any word already in uppercase it will remain the same as you can see the “E” remains the same.

9. toLowerCase

yup you get it right!!! this will make words smaller…
const sentence = ‘HELLO YO MO’;

console.log(`${sentence.toLowerCase()}`); //ans: hello yo mo

did you see I just scooched toLowerCase() with sentence. Wihout giving anything to the parenthesis, Its becase they dont need any.

10. substr

This will cut something for you to from the string…

const sentence = ‘Olodama Rasengan’;

console.log(`${sentence.substr(7,13)}`); // ans: Rasengan

here 7 is the starting index no AND 13 is ending index no.

At the end I would love to say this this String has so many Cousins/Methods who will help her out this coding world. If you feel interested find them all in the MDN docs.

--

--