As you know a JavaScript string is an object that represents a sequence of characters. JavaScript strings have methods and properties because JavaScript treats primitive values as objects when executing methods and properties.
Let’s see the list of JavaScript string methods.
Methods | Description |
---|---|
charAt() | It provides the char value present at the specified index. |
charCodeAt() | It provides the Unicode value of a character present at the specified index. |
concat() | It provides a combination of two or more strings. |
indexOf() | It provides the position of a char value present in the given string. |
lastIndexOf() | It provides the position of a char value in the given string by searching for a character from the last position. |
search() | It searches a specified regular expression in a given string and returns its position if a match occurs. |
match() | It searches a specified regular expression in a given string and returns that regular expression if a match occurs. |
replace() | It replaces a given string with the specified replacement. |
substr() | It is used to fetch the part of the given string based on the specified starting position and length. |
substring() | It fetches the part of the given string based on the specified starting position and length. |
slice() | It is used to fetch the part of the given string. It allows us to assign positive as well as negative indexes. |
toLowerCase() | It converts the given string into a lowercase letter. |
toLocaleLowerCase() | It converts the given string into an uppercase letter based on the host’s current locale. |
toUpperCase() | It converts the given string into the uppercase letter. |
toLocaleUpperCase() | It provides a string representing a particular object. |
toString() | It splits a string into a substring array, and then returns that newly created array. |
valueOf() | It provides the primitive value of a String object. |
split() | It splits a string into a substring array, then returns that newly created array. |
trim() | It trims the white space from the left and right sides of the string. |
JavaScript string methods example
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
var str="JavaScript";
console.log(str.charAt(4));
var s1 = "JS ";
var s2 = "concat example";
console.log(s1.concat(s2));
console.log(s2.indexOf("concat"));
console.log(str.toLowerCase());
console.log(str.toUpperCase());
console.log(str.slice(2,5));
console.log(s2.trim());
console.log(s2.split(" "));
</script>
</body>
</html>
Output:
Method/Property | Description | Example | Output |
---|---|---|---|
length | Returns the length of a string | let str = "Hello, world!"; str.length; | 13 |
charAt() | Returns the character at a specified index | str.charAt(0); | "H" |
charCodeAt() | Returns the Unicode of the character at a specified index | str.charCodeAt(0); | 72 |
concat() | Joins two or more strings | str1.concat(", ", str2); | "Hello, World" |
includes() | Checks if a string contains a specified value | str.includes("world"); | true |
endsWith() | Checks if a string ends with a specified value | str.endsWith("!"); | true |
indexOf() | Returns the index of the first occurrence of a specified value | str.indexOf("world"); | 7 |
lastIndexOf() | Returns the index of the last occurrence of a specified value | newStr.lastIndexOf("world"); | 20 |
match() | Searches a string for a match against a regular expression and returns the matches | text.match(/ain/g); | ["ain", "ain", "ain"] |
repeat() | Returns a new string with a specified number of copies of an existing string | str3.repeat(3); | "Hello!Hello!Hello!" |
replace() | Searches a string for a specified value and returns a new string with replaced values | text2.replace("Microsoft", "W3Schools"); | "Visit W3Schools!" |
search() | Searches a string for a specified value and returns the position of the match | text.search("SPAIN"); | 12 |
slice() | Extracts a part of a string and returns a new string | str.slice(0, 5); | "Hello" |
split() | Splits a string into an array of substrings | str4.split(", "); | ["Hello", "world!"] |
startsWith() | Checks if a string starts with a specified value | str.startsWith("Hello"); | true |
substring() | Extracts characters from a string between two specified indices | str.substring(0, 5); | "Hello" |
toLowerCase() | Converts a string to lowercase letters | str.toLowerCase(); | "hello, world!" |
toUpperCase() | Converts a string to uppercase letters | str.toUpperCase(); | "HELLO, WORLD!" |
trim() | Removes whitespace from both sides of a string | str5.trim(); | "Hello, world!" |
trimStart() | Removes whitespace from the start of a string | str5.trimStart(); | "Hello, world! " |
trimEnd() | Removes whitespace from the end of a string | str5.trimEnd(); | " Hello, world!" |
This table provides a concise reference for the various string methods and properties available in JavaScript, along with examples and their respective outputs.
Do comment if you have any doubts or suggestions on this JS methods code.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version