Mastering JavaScript | 8 STRING METHODS | part 2
Welcome back to our "Master JavaScript" course! In this video, we'll continue our exploration of essential JavaScript string methods. As we dive further into the world of strings, you'll gain a deeper understanding of how to effectively manipulate and transform text data.
Let's pick up, where we left off and cover more string methods.
The trim()
method allow to remove any leading and trailing whitespace characters from a string:
1
2
3
const text = " Hello, World! ";
const trimmedText = text.trim();
console.log(trimmedText); // Output: "Hello, World!"
if want to remove whitespace characters from leading then use thetrimStart()
method, while the trimEnd()
method do the opposite and remove the whitespace characters from trailing:
1
2
3
4
5
const text = " Hello, World! ";
const trimmedStart = text.trimStart();
console.log(trimmedStart); // Output: "Hello, World! "
const trimmedEnd = text.trimEnd();
console.log(trimmedEnd); // Output: " Hello, World!"
if the target was to add padding to the text then use the padStart()
and padEnd()
methods allow you to add padding to the beginning or end of a string until it reaches a specified length:
1
2
3
4
5
const originalText = "Hello"
const paddedStart = originalText.padStart(10, "*");
console.log(paddedStart); // Output: "*****Hello"
const paddedEnd = originalText.padEnd(10, "*");
console.log(paddedEnd); // Output: "Hello*****"
With the charAt()
method, you can retrieve the character at a specific index within a string:
1
2
3
const text = "Hello, World!";
const char = text.charAt(7);
console.log(char); // Output: "W"
Similar to charAt()
, the charCodeAt()
method returns the Unicode value of the character at a given index:
1
2
3
const text = "Hello, World!";
const unicodeValue = text.charCodeAt(0);
console.log(unicodeValue); // Output: 72 (Unicode value of "H")
final method for this video is the split()
method divides a string into an array of substrings based on a specified delimiter:
1
2
3
const text = "apple,banana,kiwi";
const fruitsArray = text.split(",");
console.log(fruitsArray); // Output: ["apple", "banana", "kiwi"]
Congratulations, you've now expanded your arsenal of JavaScript string methods! If you found this video helpful, remember to give it a thumbs up and stay tuned for more content. Happy coding, and see you in the next segment!