Essential JavaScript Techniques for React Developers — ES6 Part 03

Nipun Madusanka
2 min readJun 6, 2024

--

Photo by Safar Safarov on Unsplash

String.includes()

The includes method returns true if the specified string is found within the given string; otherwise, it returns false. Consider the following example code:

const sentence = "The quick brown fox jumps over the lazy dog";
const word = "fox";

console.log(sentence.includes(word)); // Output: true
console.log(sentence.includes("cat")); // Output: false

In this example, the includes method checks if the word “fox” is present in the sentence string and returns true because it is found. It returns false when checking for the word “cat” since it is not present in the sentence.

String.startsWith()

The startsWith method returns true if the string begins with the specified value; otherwise, it returns false. Consider the following example:

const sentence = "The quick brown fox jumps over the lazy dog";

console.log(sentence.startsWith("The")); // Output: true
console.log(sentence.startsWith("quick")); // Output: false

In this example, the startsWith method checks if the sentence string begins with “The” and returns true because it does. It returns false when checking if the string begins with “quick” since that is not the case.

String.endsWith()

The endsWith method returns true if the string ends with the specified value; otherwise, it returns false.

const sentence = "The quick brown fox jumps over the lazy dog";

console.log(sentence.endsWith("dog")); // Output: true
console.log(sentence.endsWith("fox")); // Output: false

In this example, the endsWith method checks if the sentence string ends with “dog” and returns true because it does. It returns false when checking if the string ends with “fox” since that is not the case.

Conclusion

Understanding the String methods in JavaScript, such as includes(), startsWith(), and endsWith(), is essential for efficient string manipulation. These methods provide a straightforward way to check the presence or position of substrings within a given string, making your code cleaner and more readable. By mastering these methods, you can handle common string operations with ease and improve the overall quality of your JavaScript applications.

--

--

Nipun Madusanka

Learning about new technologies is a passion of mine, and I find it enjoyable. My ultimate goal is to utilize technology in ways that benefit society.