DEV Community 👩‍💻👨‍💻

Cover image for Some basic Javascript string prototype you should know
Iftekharul Islam
Iftekharul Islam

Posted on • Updated on

Some basic Javascript string prototype you should know

Let's start
We all know that a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. Strings in JavaScript are sequences of Unicode characters. We are not going to discuss Unicode characters today.

In javascript, we write strings inside of single or double-quotes.
like,

let str1 = "hello" 
Enter fullscreen mode Exit fullscreen mode

here, hello inside double quotes are string.

Today we are going to discuss some javascript important string prototypes.

charAt()

If you want to know a character's string by using its location you have to use charAt() prototype.
you have to put the index number as a parameter then boom!
Like,

const str1 = "A good boy"
console.log(str1.charAt(4)) 
Enter fullscreen mode Exit fullscreen mode

Output : "o"

concat()

The concat() prototype concatenates the string arguments to the calling string and returns a new string. In this new string, you'll see the separate strings are in together.

Like,

const str1 = 'Bill';
const str2 = 'Gates';

console.log(str1.concat(' ', str2));
Enter fullscreen mode Exit fullscreen mode

output: "Bill Gates"

console.log(str2.concat(', ', str1));
Enter fullscreen mode Exit fullscreen mode

output: "Gates, Bill"

includes()

Sometimes we want to know whether a word is in the string or not.
So includes() method performs a case-sensitive search. It determines whether one string may be found within another string, returning true or false as appropriate.

Example,

const order = "Order of Finix"
const word = "of"
console.log(order.includes(word))
Enter fullscreen mode Exit fullscreen mode

Output: true

indexof()

If you want to know the location of a specific word or letter or value inside a string you should use indexOf().

indexOf() helps to return the index within the calling String object of the specified value, starting the search at fromIndex.
It returns -1 if the value is absent or not found.

Example,

const order = "Order of Finix"
const word = "of"
console.log(order.indexOf(word))
Enter fullscreen mode Exit fullscreen mode

Output: 6

endsWith()

By using endsWith() prototype we can determine whether a string ends with the characters of an expected string or not. It returns true or false as appropriate.

Example,

const order = "Order of Finix"
console.log(order.endsWith("Finix"))
Enter fullscreen mode Exit fullscreen mode

Output: true

lastIndexOf()

This lastIndexOf() method returns the index within the String object of the specified value which occurred last, searching backward from fromIndex.
It returns -1 if the value is not found.

Example,

const order = "Order of Finix"
const ind = 'of'
console.log(order.lastIndexOf(ind))
Enter fullscreen mode Exit fullscreen mode

Output: 6

replace()

If you want to replace a specific word or letter or RegExp with a string you have to use replace() prototype. This prototype takes two parameters, the first parameter is which you want to replace and the second parameter is by which word or letter you want to replace.

Example,

const star = "twinkle twinkle little star"
console.log(star.replace('little','big'))
Enter fullscreen mode Exit fullscreen mode

Output: "twinkle twinkle big star"

slice()

The slice() method extracts a section of a string and returns it as a new string. It doesn't modify the original string.

Example,

const star = "twinkle twinkle little star"
console.log(star.slice(7))
Enter fullscreen mode Exit fullscreen mode

Output: " twinkle little star"

console.log(star.slice(7, 14))
Enter fullscreen mode Exit fullscreen mode

Output: " twinkl"

console.log(star.slice(-14))
Enter fullscreen mode Exit fullscreen mode

Output: "le little star"

split()

If you want to divide a String into an ordered list of substrings, put these substrings into an array, and return the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.

Example,

const star = "twinkle twinkle little star"
console.log(star.split(' '))
Enter fullscreen mode Exit fullscreen mode

Output: Array ["twinkle", "twinkle", "little", "star"]

toLowerCase()

The toLowerCase() prototype returns the calling string value converted to lower case.

Example,

const country = "BANGLADESH"
console.log(country .toLowerCase())
Enter fullscreen mode Exit fullscreen mode

Output: "bangladesh"

toUpperCase()

The toUpperCase() prototype returns the calling string value converted to the Upper case.

Example,

const country = "bangladesh"
console.log(country .toUpperCase())
Enter fullscreen mode Exit fullscreen mode

Output: "BANGLADESH"

Top comments (0)

Create an Account! The only reason people scroll to the bottom...  
is because they want to read more.

Create an account to bookmark, comment, and react to articles that interest you.