Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is Substring in JavaScript?

In this blog post, we will explore a concept in JavaScript called "substring". If you are learning programming or just getting started with JavaScript, you might have come across the term "substring" and wondered what it means. In simple terms, a substring is a smaller part of a larger string. We'll dive deeper into this concept, learn how to work with substrings in JavaScript, and understand why they are useful.

What is a String?

Before we start talking about substrings, let's quickly go over what a string is. In programming, a string is a sequence of characters. A character can be a letter, number, symbol, punctuation, or even a space. Strings in JavaScript are enclosed in quotes, either single or double. For example:

let string1 = 'Hello, World!';
let string2 = "JavaScript is fun!";

Both string1 and string2 are examples of strings. They are just a sequence of characters, and we can manipulate them in various ways.

What is a Substring?

Now that we have a basic understanding of strings let's discuss substrings. A substring is a portion of a string. It can be as short as one character or as long as the entire string itself. Think of a substring as a smaller "piece" of the bigger "cake" (which is the whole string). Let's look at an example:

let mainString = "I love JavaScript!";

In the mainString above, some examples of substrings could be: - "I love" - "JavaScript" - "ve Java" - "t"

As you can see, a substring can be any smaller part of the main string.

How to Extract a Substring in JavaScript?

Now that we know what a substring is, let's learn how to extract one from a given string in JavaScript. JavaScript provides two methods for this purpose: substring() and substr().

Using substring()

The substring() method allows you to extract a substring from a given string by specifying the start and end indices. The start index is inclusive, while the end index is exclusive. This means that the extracted substring will include the character at the start index but not the character at the end index. Here's the syntax:

string.substring(startIndex, endIndex);

Let's use the substring() method to extract a substring from our mainString:

let mainString = "I love JavaScript!";
let substring = mainString.substring(2, 6);

console.log(substring); // Output: "love"

In this example, we extracted the substring "love" from our mainString. We specified the start index as 2 and the end index as 6. This means we included the character at index 2 ("l") and excluded the character at index 6 (space).

Using substr()

The substr() method is another way to extract a substring from a given string. However, instead of specifying the start and end indices, you specify the start index and the length of the substring you want to extract. Here's the syntax:

string.substr(startIndex, length);

Let's use the substr() method to extract a substring from our mainString:

let mainString = "I love JavaScript!";
let substring = mainString.substr(7, 10);

console.log(substring); // Output: "JavaScript"

In this example, we extracted the substring "JavaScript" from our mainString. We specified the start index as 7 and the length as 10. This means we started at index 7 ("J") and continued for 10 characters, which gives us the substring "JavaScript".

When to Use Substring vs. Substr?

You might be wondering which method to use when you need to extract a substring in JavaScript. The choice mainly depends on your preference and whether you find it more convenient to work with indices or lengths.

However, it is worth mentioning that the substr() method is considered a legacy feature in ECMAScript, the standard behind JavaScript. This means that it is not recommended for new projects and might be removed in future versions of the language. As a result, you might want to stick with the substring() method for your projects.

Conclusion

In this blog post, we learned about substrings in JavaScript. We discussed what strings and substrings are, and we explored how to extract substrings from a given string using the substring() and substr() methods.

As a beginner in programming, understanding and working with substrings is a crucial skill, as it allows you to manipulate and analyze textual data effectively. As you progress in your programming journey, you will encounter many situations where you need to extract and work with substrings, so make sure you understand this concept well.

Happy coding!