Skip to content

JavaScript string methods | Code

  • by

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.

MethodsDescription
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:

JavaScript string methods
Method/PropertyDescriptionExampleOutput
lengthReturns the length of a stringlet str = "Hello, world!"; str.length;13
charAt()Returns the character at a specified indexstr.charAt(0);"H"
charCodeAt()Returns the Unicode of the character at a specified indexstr.charCodeAt(0);72
concat()Joins two or more stringsstr1.concat(", ", str2);"Hello, World"
includes()Checks if a string contains a specified valuestr.includes("world");true
endsWith()Checks if a string ends with a specified valuestr.endsWith("!");true
indexOf()Returns the index of the first occurrence of a specified valuestr.indexOf("world");7
lastIndexOf()Returns the index of the last occurrence of a specified valuenewStr.lastIndexOf("world");20
match()Searches a string for a match against a regular expression and returns the matchestext.match(/ain/g);["ain", "ain", "ain"]
repeat()Returns a new string with a specified number of copies of an existing stringstr3.repeat(3);"Hello!Hello!Hello!"
replace()Searches a string for a specified value and returns a new string with replaced valuestext2.replace("Microsoft", "W3Schools");"Visit W3Schools!"
search()Searches a string for a specified value and returns the position of the matchtext.search("SPAIN");12
slice()Extracts a part of a string and returns a new stringstr.slice(0, 5);"Hello"
split()Splits a string into an array of substringsstr4.split(", ");["Hello", "world!"]
startsWith()Checks if a string starts with a specified valuestr.startsWith("Hello");true
substring()Extracts characters from a string between two specified indicesstr.substring(0, 5);"Hello"
toLowerCase()Converts a string to lowercase lettersstr.toLowerCase();"hello, world!"
toUpperCase()Converts a string to uppercase lettersstr.toUpperCase();"HELLO, WORLD!"
trim()Removes whitespace from both sides of a stringstr5.trim();"Hello, world!"
trimStart()Removes whitespace from the start of a stringstr5.trimStart();"Hello, world! "
trimEnd()Removes whitespace from the end of a stringstr5.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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading