fbpx
text

Latest JavaScript String Methods: A Comprehensive Guide

JavaScript is a versatile programming language that offers a wide range of built-in methods to manipulate strings. These methods allow developers to perform various operations on strings, such as searching, replacing, and extracting specific parts of the text. In this blog post, we will explore some of the latest JavaScript string methods along with their example codes.

1. includes()

The includes() method checks if a string contains a specific substring and returns true or false. This method is case-sensitive.

const str = "Learn JavaScript in Ranchi";
console.log(str.includes("JavaScript")); // Output: true
console.log(str.includes("Python")); // Output: false

2. startsWith()

The startsWith() method checks if a string starts with a specified substring and returns true or false. This method is case-sensitive.

const str = "Learn JavaScript in Ranchi";
console.log(str.startsWith("Learn")); // Output: true
console.log(str.startsWith("Ranchi")); // Output: false

3. endsWith()

The endsWith() method checks if a string ends with a specified substring and returns true or false. This method is case-sensitive.

const str = "Learn JavaScript in Ranchi";
console.log(str.endsWith("Ranchi")); // Output: true
console.log(str.endsWith("JavaScript")); // Output: false

4. repeat()

The repeat() method returns a new string by concatenating the original string a specified number of times.

const str = "JavaScript ";
console.log(str.repeat(3)); // Output: "JavaScript JavaScript JavaScript "

5. padStart()

The padStart() method pads the beginning of a string with a specified character until the resulting string reaches a desired length.

const str = "JavaScript";
console.log(str.padStart(15, "*")); // Output: "****JavaScript"

6. padEnd()

The padEnd() method pads the end of a string with a specified character until the resulting string reaches a desired length.

const str = "JavaScript";
console.log(str.padEnd(15, "*")); // Output: "JavaScript****"

7. trim()

The trim() method removes whitespace from both ends of a string.

const str = "   JavaScript   ";
console.log(str.trim()); // Output: "JavaScript"

8. trimStart()

The trimStart() method removes whitespace from the beginning of a string.

const str = "   JavaScript   ";
console.log(str.trimStart()); // Output: "JavaScript   "

9. trimEnd()

The trimEnd() method removes whitespace from the end of a string.

const str = "   JavaScript   ";
console.log(str.trimEnd()); // Output: "   JavaScript"

10. replace()

The replace() method replaces a specified substring with another substring.

const str = "Learn JavaScript in Ranchi";
console.log(str.replace("JavaScript", "Python")); // Output: "Learn Python in Ranchi"

These are just a few of the latest JavaScript string methods. By understanding and utilizing these methods effectively, you can enhance your string manipulation capabilities and create more powerful JavaScript applications.

Keep exploring and experimenting with JavaScript to unleash its full potential!

Scroll to Top