Some basic Javascript string prototype you should know
Iftekharul Islam
Posted on May 5, 2021
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"
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))
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));
output: "Bill Gates"
console.log(str2.concat(', ', str1));
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))
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))
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"))
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))
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'))
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))
Output: " twinkle little star"
console.log(star.slice(7, 14))
Output: " twinkl"
console.log(star.slice(-14))
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(' '))
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())
Output: "bangladesh"
toUpperCase()
The toUpperCase() prototype returns the calling string value converted to the Upper case.
Example,
const country = "bangladesh"
console.log(country .toUpperCase())
Output: "BANGLADESH"
Posted on May 5, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024