15 JavaScript String Functions

Share this article

Here is a list of all the basic JavaScript String Functions for your reference. They include functions for basic string manipulation and are very useful for jQuery scripts.

Examples of the JavaScript string functions in action

1. charAt(x) Returns the character at the “x” position within the string.
//charAt(x)
var myString = 'jQuery FTW!!!';
console.log(myString.charAt(7));
//output: F
2. charCodeAt(x) Returns the Unicode value of the character at position “x” within the string.
//charAt(position)
var message="jquery4u"
//alerts "q"
alert(message.charAt(1))
3. concat(v1, v2,…) Combines one or more strings (arguments v1, v2 etc) into the existing one and returns the combined string. Original string is not modified.
//concat(v1, v2,..)
var message="Sam"
var final=message.concat(" is a"," hopeless romantic.")
//alerts "Sam is a hopeless romantic."
alert(final)
4. fromCharCode(c1, c2,…) Returns a string created by using the specified sequence of Unicode values (arguments c1, c2 etc). Method of String object, not String instance. For example: String.fromCharCode().
//fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97,98,99,120,121,122))
//output: abcxyz
console.log(String.fromCharCode(72,69,76,76,79))
//output: HELLO
//(PS - I have no idea why you would use this? any ideas?)
Also see: Full List of JavaScript Character Codes 5. indexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is 0.
//indexOf(char/substring)
var sentence="Hi, my name is Sam!"
if (sentence.indexOf("Sam")!=-1)
alert("Sam is in there!")
6. lastIndexOf(substr, [start]) Searches and (if found) returns the index number of the searched character or substring within the string. Searches the string from end to beginning. If not found, -1 is returned. “Start” is an optional argument specifying the position within string to begin the search. Default is string.length-1.
//lastIndexOf(substr, [start])
var myString = 'javascript rox';
console.log(myString.lastIndexOf('r'));
//output: 11
7. match(regexp) Executes a search for a match within a string based on a regular expression. It returns an array of information or null if no match is found.
//match(regexp) //select integers only
var intRegex = /[0-9 -()+]+$/;  

var myNumber = '999';
var myInt = myNumber.match(intRegex);
console.log(isInt);
//output: 999

var myString = '999 JS Coders';
var myInt = myString.match(intRegex);
console.log(isInt);
//output: null
Also see: jQuery RegEx Examples to use with .match() 8. replace(regexp/substr, replacetext) Searches and replaces the regular expression (or sub string) portion (match) with the replaced text instead.
//replace(substr, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(/JavaScript/i, "jQuery"));
//output: 999 jQuery Coders

//replace(regexp, replacetext)
var myString = '999 JavaScript Coders';
console.log(myString.replace(new RegExp( "999", "gi" ), "The"));
//output: The JavaScript Coders
9. search(regexp) Tests for a match in a string. It returns the index of the match, or -1 if not found.
//search(regexp)
var intRegex = /[0-9 -()+]+$/;  

var myNumber = '999';
var isInt = myNumber.search(intRegex);
console.log(isInt);
//output: 0

var myString = '999 JS Coders';
var isInt = myString.search(intRegex);
console.log(isInt);
//output: -1
10. slice(start, [end]) Returns a substring of the string based on the “start” and “end” index arguments, NOT including the “end” index itself. “End” is optional, and if none is specified, the slice includes all characters from “start” to end of string.
//slice(start, end)
var text="excellent"
text.slice(0,4) //returns "exce"
text.slice(2,4) //returns "ce"
11. split(delimiter, [limit])
Splits a string into many according to the specified delimiter, and returns an array containing each element. The optional “limit” is an integer that lets you specify the maximum number of elements to return.
//split(delimiter)
var message="Welcome to jQuery4u"
//word[0] contains "We"
//word[1] contains "lcome to jQuery4u"
var word=message.split("l")
12. substr(start, [length]) Returns the characters in a string beginning at “start” and through the specified number of characters, “length”. “Length” is optional, and if omitted, up to the end of the string is assumed.
//substring(from, to)
var text="excellent"
text.substring(0,4) //returns "exce"
text.substring(2,4) //returns "ce"
13. substring(from, [to]) Returns the characters in a string between “from” and “to” indexes, NOT including “to” inself. “To” is optional, and if omitted, up to the end of the string is assumed.
//substring(from, [to])
var myString = 'javascript rox';
myString = myString.substring(0,10);
console.log(myString)
//output: javascript
14. toLowerCase() Returns the string with all of its characters converted to lowercase.
//toLowerCase()
var myString = 'JAVASCRIPT ROX';
myString = myString.toLowerCase();
console.log(myString)
//output: javascript rox
15. toUpperCase() Returns the string with all of its characters converted to uppercase.
//toUpperCase()
var myString = 'javascript rox';
myString = myString.toUpperCase();
console.log(myString)
//output: JAVASCRIPT ROX

Frequently Asked Questions (FAQs) about JavaScript String Functions

What is the difference between charAt() and charCodeAt() in JavaScript?

The charAt() and charCodeAt() are both JavaScript string methods but they serve different purposes. The charAt() method returns the character at a specified index in a string. For example, if you have a string “Hello” and you use the charAt(0) method, it will return “H” which is the first character in the string. On the other hand, the charCodeAt() method returns the Unicode of the character at a specified index in a string. So, if you use charCodeAt(0) on the same string “Hello”, it will return 72 which is the Unicode value for “H”.

How can I convert a string to an array in JavaScript?

JavaScript provides the split() method to convert a string into an array. The split() method divides a string into an ordered set of substrings, puts these substrings into an array, and returns 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.

What is the purpose of the trim() function in JavaScript?

The trim() function in JavaScript is used to remove whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.). This function does not affect the value of the string itself, it returns a new string.

How can I replace a specific character in a string using JavaScript?

You can use the replace() method in JavaScript to replace a specific character or a sequence of characters in a string. The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

How can I convert a string to lowercase or uppercase in JavaScript?

JavaScript provides two methods to convert a string to lowercase or uppercase. The toLowerCase() method converts a string to lowercase letters, and the toUpperCase() method converts a string to uppercase letters.

What is the difference between slice() and substring() in JavaScript?

Both slice() and substring() methods extract parts of a string and return the extracted parts in a new string. The main difference between these two methods is that slice() can accept negative indices, but substring() cannot. If a negative or non-numeric argument is passed to substring(), it will be treated as if it were 0.

How can I check if a string contains a specific word in JavaScript?

You can use the includes() method in JavaScript to check if a string contains a specific word or not. The includes() method determines whether a string contains the characters of a specified string. This method returns true if the string contains the characters, and false if not.

How can I repeat a string a certain number of times in JavaScript?

JavaScript provides the repeat() method to construct and return a new string which contains the specified number of copies of the string on which it was called, concatenated together.

How can I convert a number to a string in JavaScript?

You can use the toString() method in JavaScript to convert a number to a string. This method returns a string representing the specified Number object.

How can I find the length of a string in JavaScript?

You can use the length property in JavaScript to get the length of a string. The length property returns the number of characters in a string.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week