Introduction
In this article we will learn various string object methods in JavaScript. As we know JavaScript is an object-oriented language and everything is treated as an object in JavaScript. The string is also one type of object in JavaScript.
|
Method
|
Description
|
|
Returns the position of the first found occurrence of a specified value in a string
|
|
|
Returns the character at the specified index
|
|
|
Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
|
|
|
Extracts the characters from a string, between two specified indices
|
|
|
Converts a string to lowercase letters
|
|
|
Converts a string to uppercase letters
|
Let us understand one by one with an example.
Index of Function
The indexOf function returns the position of the first occurrence of a specified value in a string. In the following example we are trying to find the index of "s" in the "a" string.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RamaSagar";
- alert(a.indexOf('S'));
- </script>
- </form>
- </body>
- </html>
Output

charAt() Function
This function returns the character at a specific position. It takes one integer argument specifying an index of the string. Here is a code snippet to understand the charAt() function.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var a = "RamaSagar";
- alert(a.charAt(2));
- </script>
- </form>
- </body>
- </html>






Join the conversation! Your thoughts help the community grow.