String functions in JavaScript

Ankit P. Karve
4 min readJun 1, 2021

--

JavaScript is becoming more important day by day as new frameworks of JavaScript are getting introduced. Node, react, angular….No matter which framework you are using, having good command over JavaScript is always helpful.

So while doing project in JavaScript or JavaScript based framework, we always need to work with String, arrays. To perform common operations String object in JavaScript have very useful methods like slice(), replace(), match(), substr() that are useful while writing JS code.

slice(): slice method extracts section from provided start and end index. and this both index are optional. it means if we don’t provide any index it will return string all section of string. it is also having capability to work with negative indexes. when negative index start cursor get shift to end as per negative index. and this method doesn’t modify original object.

slice() example

replace(): it takes two arguments search value and replacement. search value can be string or regExp. If it is a string then it will replace only 1 occurrence. We can replace all occurrence using global expression.

replace() example

trim(): trim usually removes whitespaces from trailing and ending part of string. To trim from only one side we can use trimStart(), trimEnd() , methods. it also doesn’t affect original object.

trim() example

substr(): substr() function used to extract portion of string from original string. It take two parameter starting index and length of characters to extract. second parmter is option if is not provided it will go till end of string.

substr() example

substring(): substring also extracts portion but it have two parameter start index and end index. so it will extract between those index.

substring() example

As you can see in above example passing 14,20 to substr() and substring() produce different result. because 2nd parameter is main difference between substr() and substring(). substr() 2nd paramter is no. of characters to return from start index. and substring() 2nd parameter specifies end index of string.

split(): It seperates string from provided pattern and return array of that seperated substrings. It takes two argument, first is string or regExp which will be used in separating and second parameter is number of item to return which is optional parameter.

split() example

match(): it take one argument string or regExp and return array consisting result of our search criteria. It means if we have passed regExp to match digit it will retrieve those values in array. If no match found it will return null.

match() example

charCodeAt(): it take one argument which is index of character and returns integer value between 0 to 65535 which represent UTF-16 unit.

charCodeAt() example

includes(): it takes two parameter, first is search string , second is from which position searching should start. second parameter is optional. If provided substring found in string then it returns true.

includes() example

******************************************************************

Hope so this is helpful to you. If you liked my article and you want to know more about my future articles, you can follow me in Medium.

--

--

No responses yet