What is a String Substring() Method in JavaScript?

Are you curious about what is substring() method in JavaScript? Read on to learn how to use it effectively for string manipulation.

JavaScript provides several methods to work with substrings, but one of the most commonly used is the substring() method.

In this article, we will hand you the solutions how to extract, check, and finding substrings within strings using JavaScript built-in methods.

So without further ado, let’s get started!

What is a substring in JavaScript?

The substring() method in JavaScript is used to extract a portion of a given string.

You can think of it as a smaller string that exists within a larger string. For instance, in the string “Itsourcecode,” “code” is a substring.

This extraction is based on two indices: the starting index (inclusive) and the ending index (exclusive).

By specifying these indices, you can effortlessly obtain a substring that contains the characters within the specified range.

Here’s how it works:

let samplestring = "Hi, Welcome to Itsourcecode.com";
let subString = samplestring.substring(4, 27); ✅
console.log(subString); 

Output:

Welcome to Itsourcecode

Here’s another example:

let samplestring = "Hi, Welcome to Itsourcecode.com";
let subString = samplestring.substring(0, 2);  
console.log(subString); 

Output:

Hi

Syntax

Here’s the syntax of substring() method:

string.substring(startIndex, endIndex);

Parameters

The string which the original string from which the substring will be extracted has two parameters:

startIndex (Required) 

The index at which the extraction will begin (inclusive).

endIndex (Optional)

The index at which the extraction will end (exclusive).

Return value

The method returns the extracted substring as a new string, leaving the original string unchanged.

📌 It’s important to note that the substring() method does not modify the original string.

Instead, it returns a new string that represents the extracted substring.

How to check a string contains a substring in JavaScript?

You can check if a string contains a substring in JavaScript, using the includes() method.

This method determines whether one string may be found within another string, returning true or false as appropriate.

Here’s an example:

let samplestring = "Hi, Welcome to Itsourcecode.com";
let subString = "Itsourcecode";
let result = samplestring.includes(subString);✅ 
console.log(result);

Output

true

In this example, samplestring.includes(subString) checks if the string samplestring contains the substring subString.

The result is true because “Itsourcecode” is indeed a substring of “Hi, Welcome to Itsourcecode.com.”

If the substring is not found in the string, includes() will return false.

For example:

let samplestring = "Hi, Welcome to Itsourcecode.com";
let subString = "Hello";
let result = samplestring.includes(subString);✅ 
console.log(result);

Output:

false

In this case, the output is false because Hello is not a substring of “Hi, Welcome to Itsourcecode.com.”

How to find substring in string JavaScript?

You can find a substring within a string in JavaScript, using the indexOf() method.

This method returns the index of the first occurrence of a specified text in a string. If the specified text is not found, it returns -1.

Here’s an example:

let samplestring = "Hi, Welcome to Itsourcecode.com";
let subString = "Itsourcecode";
let index = samplestring.indexOf(subString); ✅
console.log(index);

Output:

15

In this example, samplestring.indexOf(subString) finds the index of the first occurrence of the substring subString in the string samplestring.

The result is 15 because “Itsourcecode” starts at index 15 in “Hi, Welcome to Itsourcecode.com.”

If the substring is not found in the string, indexOf() will return -1.

Here’s an example:

let samplestring = "Hi, Welcome to Itsourcecode.com";
let subString = "hello";
let index = samplestring.indexOf(subString);
console.log(index);

Output:

-1


In this case, the output is -1 because “hello” is not found in “Hi, Welcome to Itsourcecode.com.”

Conclusion

In conclusion, the substring() method in JavaScript is used for extracting specific portions of a string based on defined indices.

It allows you to create a new substring without modifying the original string. By using the includes() method, you can efficiently check whether a string contains a particular substring, returning true or false.

These string manipulation techniques are essential for various JavaScript applications, enabling you to manipulate and analyze text effectively.

We are hoping that this article provides you with enough information that helps you understand what is a substring in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment