ES6 String Methods 🎯

beginner
7 min

ES6 String Methods 🎯

Welcome to our comprehensive guide on ES6 String Methods! In this lesson, we'll dive deep into various methods that make working with strings in JavaScript easier and more efficient. By the end of this tutorial, you'll be equipped with practical knowledge to handle strings like a pro! 🚀

What are String Methods? 📝

String methods are functions that allow you to manipulate, search, and transform strings in JavaScript. They help in various tasks, such as converting text to uppercase or lowercase, finding a specific character in a string, and splitting a string into an array.

Basic String Methods 💡

Let's start with some basic string methods that every developer should know:

length

The length property returns the number of characters in a string.

javascript
let myString = "Hello, World!"; console.log(myString.length); // Output: 13

charAt()

The charAt() method returns the character at the specified index.

javascript
let myString = "Hello, World!"; console.log(myString.charAt(0)); // Output: H console.log(myString.charAt(7)); // Output: ,

indexOf()

The indexOf() method returns the index of the first occurrence of the specified value. If the value is not found, it returns -1.

javascript
let myString = "Hello, World!"; console.log(myString.indexOf("World")); // Output: 7 console.log(myString.indexOf("Worl")); // Output: -1

slice()

The slice() method returns a new string that is a portion of an existing string.

javascript
let myString = "Hello, World!"; console.log(myString.slice(0, 5)); // Output: Hello console.log(myString.slice(7)); // Output: World!

substring()

The substring() method returns a new string that is a portion of an existing string. The method works similar to the slice() method but with some differences in behavior when the starting index is greater than the ending index.

javascript
let myString = "Hello, World!"; console.log(myString.substring(0, 5)); // Output: Hello console.log(myString.substring(7)); // Output: World! console.log(myString.substring(7, 4)); // Output: World (note the different behavior when the ending index is smaller than the starting index)

Advanced String Methods 💡

Now that we've covered the basics, let's explore some advanced string methods:

replace()

The replace() method replaces instances of a specified value with a new value.

javascript
let myString = "Hello, World!"; console.log(myString.replace("World", "CodeYourCraft")); // Output: Hello, CodeYourCraft!

split()

The split() method splits a string into an array of substrings.

javascript
let myString = "Hello, World!"; console.log(myString.split(" ")); // Output: ["Hello", ",", "World!"]

toUpperCase() and toLowerCase()

The toUpperCase() and toLowerCase() methods convert all the characters in a string to uppercase or lowercase, respectively.

javascript
let myString = "Hello, World!"; console.log(myString.toUpperCase()); // Output: HELLO, WORLD! console.log(myString.toLowerCase()); // Output: hello, world!

Quiz 💡

Quick Quiz
Question 1 of 1

Which string method returns the index of the first occurrence of the specified value?

That's it for our ES6 String Methods tutorial! Practice using these methods in your projects to become more comfortable with manipulating strings in JavaScript. Happy coding! 🥳