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 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment