10 Most Common Javascript String Function

Raihan Uddin
4 min readNov 2, 2020

--

These functions you will need in your regular programming life

Most Common Javascript String Function

charAt(): This javascript method is used to find the specific character of a “string” by an index number. Characters of a string formed by index number, which is started from 0. This charAt() method take a parameter which is actually the index number. If no index is provided as a parameter, it will take 0 by default. Ex:

Syntax: yourString.charAt( indexnumber )

const greeting = “Hello World”;const indexNum = 3;console.log(“The character of ”+ indexNum + “ is “+greeting.charAt(indexNum));//Result of this code is: lconsole.log(greeting.charAt());//Result of this log is: H//This is default result.

concat(): This method is used to combine two or more strings in one. It creates a new string, does not affect the other string. Ex:

Syntax: oneString.concat(otherString);

const str1 = “Hello”;const str2 = “world”;const greeting = str1.concat(‘ ‘,str2);console.log(greeting);//Result is: “Hello world”

includes(): This method is useful to check if a “string” includes a specific word. This method actually returns a boolean result. That means true or false. If the specific word can be found in a string it will return true otherwise return false. Ex:

Syntax: string.includes(‘your word’);

const sentence = “The name of our country is Bangladesh.”;const word = “bangladesh”;console.log(sentence.includes(word));//This will return true. Because (word) is included in the sentence

endsWith(): This method helps to know whether a string ends with another string or not. This also returns a boolean result. If the searchValue is matched with the end of the specified string then it will return true otherwise false. It takes two parameters. One is searchValue which required and another is searching value length (this is optional).

Syntax: string.endsWith(‘search’,length)

const sentence = “The name of our country is Bangladesh”;const word = “Bangladesh”;console.log(sentence.endsWith(word));//result is trueconsole.log(sentence.endsWith(word,5));//result is false. Because length of the word not matched.console.log(sentence.endsWith(‘country’,6));// result is false. Searching word not matched

indexOf(): This method is used to find the position of a character in a string. It returns a number which is the position of the enquired character. If the character is not found in the string then it will return -1 that means the character does not exist in the string.

If I want to know the index number of “e” in a string and the string includes more than one “e”, the result will be the index number of first “e”. This method takes two parameters, one is search value, another is the starting position of search value (This is optional).

Syntax: string.indexOf(‘search value’,5(starting position))

const sentence = “The name of our country is Bangladesh”;const word = “B”;console.log(sentence.indexOf(word));// result is: 27const word = “a”;console.log(sentence.indexOf(word));// result is: 5const word = “a”;console.log(sentence.indexOf(word, 10));// In that case result is : 28. Because my searching position started from 10. That’s why first “a” is not counted.

lastIndexOf(): This method also returns index number. But it returns the position of the last occurrence of a value in a string. This method is most likely indexOf() method. IndexOf method returns the position of the first occurrence. On the other hand, lastIndexOf method returns the last occurrence of searching value. lastIndexOf() also takes two parameters, one is search item (This is required) and another is ‘starting point’ (this is optional).

Ex:

Syntax: string.lastIndexOf(‘search’, starting index)

const sentence = ‘The name of our country is Bangladesh. I live in Bangladesh’;const word = ‘Bangladesh’;console.log(sentence.lastIndexOf(‘Bangladesh’));//Result is: 49. This is last position of Bangladeshconsole.log(sentence.lastIndexOf(‘bangladesh’));//Result is: -1 . That means bangladesh not found in the sentence. Because this is case sensitive method

replace(): To replace a value or values of a string by new a string, replace() method can be used. It can perform the global replacement. That means every exact word in the string matched with “value” will be replaced. This is a case sensitive method. But It can also be performed as case insensitive. It returns a new string and doesn’t affect the old string. Let’s do some example. This will be more clear after example.

Syntax: string.replace(old string’,’new string’)

const sentence = “The quick brown fox jumps over the lazy dog. Dog is a faithful animal.”;console.log(sentence.replace(‘dog’,’cat’));//result: The quick brown fox jumps over the lazy cat. Dog is a faithful animal.// Just replaced the first dog. But ‘Dog’ is not changed. Because replace is not performing globally and there has also case-sensitivity issue.console.log(sentence.replace(/dog/gi,’cat’));//result: The quick brown fox jumps over the lazy cat. cat is a faithful animal.// g — used for replace() perform globally// i — use for replace() perform case insensitively.

slice(): This method helps to cut and create a new string from a specified string. It returns new string and doesn’t affect the old one. It takes two value. One is the starting index and another is ending index. By default, it starts at 0.Syntax: string.slice(10,15)

const string = ‘The quick brown fox jumps over the lazy dog.’;console.log(string.slice(0,10));// result : The quick

split(): It divides a string by specific order. It can be white space ( ‘ ‘) or Hypen ( — ) etc. After that it provides an array for every words.

--

--