String methods help you to work with strings. I have curated a list with 8 common methods which you can come across in every code. Now let's get our hands dirty by looking at some code.
A string is converted to upper case with toUpperCase()
and to lower case withtolowercase()
.
let a = "Kaarthik";
console.log(a.toUpperCase()); // a is converted to uppercase -> KAARTHIK
let b = "SEkaR";
console.log(b.toLowerCase()); // b is converted to lowercase -> sekar
The length
property returns the length of a string:
let name = "Kaarthik";
console.log(name.length); // 8
The slice()
extracts a part of a string and returns the extracted part in a new string and it also returns the shallow copy which you can save in a new variable.
syntax:
slice(start, end)
Now let's see an example,
let str = ['apple','Mango','Orange'];
console.log(str.slice(0,1)); // Array [ "apple" ]
The substring()
method returns the part of the string between the start and end indexes, or to the end of the string.
IndexStart
- The index of the first character to include in the returned substring.IndexEnd
- The index of the first character to exclude from the returned substring.