In this article, we will be discussing the 15 JavaScript string methods you need to know. Strings are treated as objects in JavaScript. So, methods and properties are also available to strings in JavaScript. JavaScript string methods help us to manipulate the strings.
Prerequisites
- Basic understanding of JavaScript.
Here are the 15 JavaScript String Methods You Need To Know:
1. length()
length
method returns the length of the string.
Syntax
string.length
Example
let str = "ScanSkill";
let length = str.length;
console.log(length)
Output
10
2. toLowerCase()
toLowerCase()
method converts a string to lowercase.
Syntax
string.toLowerCase()
Example
let str = "Welcome TO ScanSKILL";
let result = str.toLowerCase();
console.log(result)
Output
welcome to scanskill
3. toUpperCase()
toUpperCase()
method converts a string to uppercase.
Syntax
string.toUpperCase()
Example
let str = "Welcome TO ScanSKILL";
let result = str.toUpperCase();
console.log(result)
Output
WELCOME TO SCANSKILL
4. startsWith()
startsWith()
checks whether or not a string starts with the given string. It is case-sensitive while checking the string. It returns a boolean value.
Syntax
string1.startsWith(string2)
Example
let str = "Welcome to ScanSkill";
let result1 = str.startsWith("welcome");
let result2 = str.startsWith("Welcome");
let result3 = str.startsWith("Wel");
console.log(result1);
console.log(result2);
console.log(result3);
Output
false
true
true
5. endsWith()
endsWith()
checks whether or not a string starts with the given string. It is case-sensitive while checking the string. It returns a boolean value.
Syntax
string1.endsWith(string2)
Example
let str = "Welcome to ScanSkill";
let result1 = str.endsWith("Skill");
let result2 = str.endsWith("skill");
let result3 = str.endsWith("kill");
console.log(result1);
console.log(result2);
console.log(result3);
Output
true
false
true
6. concat()
The concat()
method concatenates the given strings. It joins the given strings to form a single string value.
Syntax
string1.concat(string2,string3,...);
Example
let str1 = "Welcome"
let str2 = " to "
let str3 = "ScanSkill"
let result1 = str1.concat(str2)
let result2 = str1.concat(str2,str3)
console.log(result1);
console.log(result2);
Output
Welcome to
Welcome to ScanSkill
7. indexOf()
The indexOf()
returns the index of the first appearance of the specified character or string in the given string.
Syntax
string1.indexOf(string2)
Example
let str = "Welcome to ScanSkill"
let result1 = str.indexOf("S")
let result2 = str.indexOf("to")
console.log(result1);
console.log(result2);
Output
11
8
8. lastIndexOf()
The lastIndexOf()
returns the index of the last appearance of the specified character or string in the given string.
Syntax
string1.lastIndexOf(string2)
Example
let str = "Welcome to ScanSkill"
let result1 = str.lastIndexOf("S")
let result2 = str.indexOf("me")
console.log(result1);
console.log(result2);
Output
15
5
9. charAt()
The charAt()
method returns the character at a specified index in a string.
Syntax
string.charAt(index)
Example
let str = "Welcome to ScanSkill"
let result1 = str.charAt(0)
let result2 = str.charAt(12)
console.log(result1);
console.log(result2);
Output
W
c
10. replace()
The replace()
method replaces a specified value with another value in a string. The replace()
method replaces only the first match in the string.
Syntax
string.replace(prevValue, newValue)
Example
let str = "Welcome to ScanSkill"
let result = str.replace("to", "at")
console.log(result);
Output
Welcome at ScanSkill
11. split()
The split() method splits a string from the provided character. It returns an array of split strings.
Syntax
string.split(character)
Example
let str = "Welcome to ScanSkill"
let result1 = str.split(" ")
let result2 = str.split("to")
console.log(result1);
console.log(result2);
Output
[ 'Welcome', 'to', 'ScanSkill' ]
[ 'Welcome ', ' ScanSkill' ]
12. slice()
The slice()
method cuts a part of a string and returns the sliced part as a new string. It takes two parameters: the start index, and the end index(end not included).
Syntax
string.slice(startIndex, endIndex)
Example
let str = "Welcome to ScanSkill"
let result1 = str.slice(6,15)
let result2 = str.slice(0,6)
console.log(result1);
console.log(result2);
Output
e to Scan
Welcom
13. trim()
The trim()
removes the white spaces from both sides of the string.
Syntax
string.trim()
Example
let str = " Welcome to ScanSkill "
let result = str.trim();
console.log(result);
Output
Welcome to ScanSkill
14. includes()
The includes()
method checks whether or not the string includes the specified character(s). It returns a boolean value.
Syntax
string.includes(character)
Example
let str = "Welcome to ScanSkill"
let result1 = str.includes("to");
let result2 = str.includes("z");
console.log(result1);
console.log(result2);
Output
true
false
15. match()
The match()
method checks whether the string matches the provided regular expression.
Syntax
string.match(expression)
Example
let email1 = "hello@scanskill.com";
let email2 = "hello.scanskill.com";
let regex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
let result1 = email1.match(regex);
let result2 = email2.match(regex);
console.log("email1 =>",result1);
console.log("email2 =>",result2);
Output
email1 => [ 'hello@scanskill.com',
'hello',
'hello',
undefined,
undefined,
'scanskill.com',
'scanskill.',
index: 0,
input: 'hello@scanskill.com',
groups: undefined ]
email2 => null
Conclusion
In this article, we discussed the 15 JavaScript string methods you need to know. These string methods can be useful whenever we need to manipulate a string.