String Methods-tutorial
In JavaScript, string methods help us manipulate and work with text easily.
Below are some of the most common and useful string methods every beginner should know.
charAt(index) Method
The charAt() method returns the character at the specified index (position starts from 0).
Example:
Output
.webp)
concat(str) Method
The concat() method is used to join two or more strings together.
Example:
Output
.webp)
indexOf(str) Method
The indexOf() method returns the index position of the first occurrence of a specified substring.
If the substring is not found, it returns -1.
Example:
Output
.webp)
lastIndexOf(str) Method
The lastIndexOf() method returns the index position of the last occurrence of a specified substring.
Example:
Output
.webp)
charAt(index) Method
The charAt() method returns the character at the specified index (position starts from 0).
Example:
<script> var str = "javascript"; document.write(str.charAt(2)); </script>
Output
.webp)
concat(str) Method
The concat() method is used to join two or more strings together.
Example:
<script> var s1 = "javascript "; var s2 = "concat example"; var s3 = s1.concat(s2); document.write(s3); </script>
Output
.webp)
indexOf(str) Method
The indexOf() method returns the index position of the first occurrence of a specified substring.
If the substring is not found, it returns -1.
Example:
<script> var s1 = "javascript from javatpoint indexof"; var n = s1.indexOf("from"); document.write(n); </script>
Output
.webp)
lastIndexOf(str) Method
The lastIndexOf() method returns the index position of the last occurrence of a specified substring.
Example:
<script> var s1 = "javascript from javatpoint indexof"; var n = s1.lastIndexOf("java"); document.write(n); </script>
Output
.webp)
Tip: These methods make it easy to handle text dynamically in web applications.