String methods are built-in functions used to work with text. They help us find the length, change letters, search words, extract characters, and more.
1.Length :
Length - property returns the length of the string.
let name = "Hello";
let length = name.length; //5
console.log(name.length); //5
2.toLowerCase() :
convert all letters into small letters.
let name = "JAVASCRIPT";
console.log(name.toLowerCase()); //javascript
3.toUpperCase() :
convert all letters into capital letters.
let name = "javascript";
console.log(name.toUpperCase()); //JAVASCRIPT
- there are 4 methods to get string character
4.at() :
method returns the character at a specified index (position) in a string.
const name = "Samsung";
let letter = name.at(2); //m
note: It allows the use of negative indexes while charAt() do not.
const name = "sowntharya";
let letter = name.at(-5); //h
5.CharAt() :
method returns the character at a specified index in a string.
let text = "HELLO WORLD";
let char = text.charAt(0); //H
6.charCodeAt() :
method returns the code of the character at a specified index in a string.
returns a UTF-16 code (an integer between 0 and 65535).
let text = "HELLO WORLD";
let char = text.charCodeAt(0); //72
7.Propert Access [] :
let text = "HELLO WORLD";
let char = text[0]; //H
note : If no character is found, [ ] returns undefined, while charAt() returns an empty string. It looks like array but they are not.
8.concat() :
join/combine two or more strings.
let text1 = "Sam";
let text2 = "Sung";
let text3 = text1.concat(" ", text2); //Sam Sung
note: empty quotes for space between 2 string
strings are immutable: Strings cannot be changed, only replaced.
- To get part a of string 3 methods
9.slice () :
extracts a part of a string and returns the extracted part in a new string.
note : take 2 parameters: start position, and end position
First position is 0. Second position is 1
let text = "Apple, Banana, Kiwi";
let part = text.slice(7, 13); // Banana
If you omit the second parameter, the method will slice out the rest of the string:
let text = "Apple, Banana, Kiwi";
let part = text.slice(7); //Banana, Kiwi
If a parameter is negative, the position is counted from the end of the string
let text = "Apple, Banana, Kiwi";
let part = text.slice(-12); //Banana, Kiwi
10.substring() :
substring() is similar to slice().
The difference is that start and end values less than 0 are treated as 0 in substring().
let str = "Apple, Banana, Kiwi";
let part = str.substring(7, 13); //Banana
11.substr() :
The second parameter specifies the length of the extracted part.
same as slice().
If you omit the second parameter, the method will slice out the rest of the string:
let text = "Apple, Banana, Kiwi";
let part = text.slice(7); //Banana, Kiwi
If a parameter is negative, the position is counted from the end of the string
let text = "Apple, Banana, Kiwi";
let part = text.slice(-12); //Banana, Kiwi
note : substr() not removed from JS.
- substr() is still supported by modern JavaScript engines.
- Old features are documented in Annex B.
- developers are advised not to use it in new code.
- It may still work for backward compatibility.
Top comments (2)
Nice to see your Javascript series growing! One small detail is that length is a property rather than a method, which is why you write name.length without parentheses while toLowerCase() and toUpperCase() need them. You might enjoy slice(), includes() and replace() next, since they cover the searching and extracting you mention in the intro. Which topic is coming next in the series?
Thank you so much for taking the time to read my post and share your valuable feedback! I really appreciate the suggestions. I'll definitely continue exploring and sharing more JavaScript string methods in the upcoming posts.