JavaScript String Method
The javascript string methods are very useful in manipulating strings, i.e. doing changes in them , like adding one string to another, converting case of the characters of the strings, etc.
Let's see the list of mostly used JavaScript string methods with examples.
Method |
Description |
charAt(index) |
It returns the character present at the specified index. |
concat(str) |
It is used to combine any two strings and returns a new string. |
indexOf(str) |
It returns the index position of the specified string. |
toLowerCase() |
It converts the string characters to lower case. |
toUpperCase() |
It converts the string characters to upper case. |
JavaScript String charAt(index) Method
Every character present in a string has an index value like as of an array. The index value starts with '0'
and it can be used to access any character in the string. The charAt(index)
method works on this index number and returns the character which is present at the specified index.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String charAt Method </title>
</head>
<body>
<script>
var str="javascript";
document.write(str.charAt(5));
</script>
</body>
</html>
Output
JavaScript String charAt Method
JavaScript String concat(str) Method
This method combines
any two different strings together and returns a new string comprising of both of them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String concat Method </title>
</head>
<body>
<script>
var str1="This is an Example of";
var str2=" concat Method";
var str3=str1.concat(str2);
document.write(str3);
</script>
</body>
</html>
Output
JavaScript String concat Method Example
JavaScript String indexOf(str) Method
If we want to know the index number of any character or part of a string then this indexOf(str)
method can be used. This function returns the index value of the specified part of string or character present in a string, or if it doesn't finds any match then it returns '-1'
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String indexOf Method </title>
</head>
<body>
<script>
var str1="This is an Example of indexOf Method";
var n=str1.indexOf("of");
document.write(n);
</script>
</body>
</html>
Output
JavaScript String indexOf Method
JavaScript String toLowerCase() Method
As the name suggests this method converts all specified string's characters to lower case characters. The resultant string will have all the characters in lowercase.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String toLowercase Method </title>
</head>
<body>
<script>
var str1="This is an Example of JavaScript toLowerCase Method";
var str2=str1.toLowerCase();
document.write(str2);
</script>
</body>
</html>
Output
JavaScript String toLowercase Method Examples
JavaScript String toUpperCase() Method
This method does opposite of the previous one. It converts all specified string's characters to upper case characters.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String toUpperCase Method </title>
</head>
<body>
<script>
var str1="This is an Example of JavaScript toLowerCase Method.";
var str2=str1.toUpperCase();
document.write(str2);
</script>
</body>
</html>
Output
JavaScript String toUpperCase Method Example
JavaScript String Length
If we want to know the length of a particular string then we can use the length
property. This property returns the length(total no. of characters) of a string.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String Length Method </title>
</head>
<body>
<script>
var str1 = "This is a paragraph of text.";
document.write(str1.length + "<br>"); // Prints 28
var str2 = "This is a \n paragraph of text.";
document.write(str2.length); // Prints 30, because \n is only one character
</script>
</body>
</html>
Output
JavaScript String Length Method