How to Remove a Part of String in JavaScript?

Are you ready to discover and explore the solutions on how to remove a part of the string in JavaScript?

In this article, we will hand you several solutions that will help you in removing the substring or part of the string.

So, without further ado, let’s get started!

What is JavaScript string?

A JavaScript string is a sequence of characters used to represent text. It is one of the primitive data types in JavaScript, along with numbers, booleans, null, and undefined.

Strings can be created using either single (‘ ‘) or double (” “) quotes, and they have many built-in methods that allow you to manipulate and work with them.

For instance, you can use the length property to find the length of a string, the indexOf() method to search for a substring within a string, or the substring() method to extract a part of a string.

Solutions on how to remove part of string in JavaScript

Here are some more examples of how to remove part of a string in JavaScript:

Solution 1: Using the split() and pop() methods

You can split the string into an array of strings using a specified separator string, then use the pop() method to remove the last element from the array and return that element.

For example:

let samplestr = "Welcome to Itsourcecode!";
let arr = samplestr.split(" ");
let laststr = arr.pop();
console.log(laststr); 

Output:

Itsourcecode!

Solution 2: Use the substring() method

You can use the substring() method to extract a part of a string and return it as a new string.

For example:

let samplestr = "Itsourcecode gives you free tutorials!";
let laststr = samplestr.substring(samplestr.lastIndexOf(" ") + 1);
console.log(laststr); 

Output:

tutorials!

Here’s another example:

const samplestr = "Welcome to Itsourcecode!";
const startIndex = 10; // Start index of the part you want to keep
const endIndex = 23;   // End index of the part you want to keep

const newsampleStr = samplestr.substring(startIndex, endIndex); ✅
console.log(newsampleStr); 

Output:

 Itsourcecode

Solution 3: Use template literals

You can use template literals to create a new string by concatenating parts of the original string with other strings or expressions.

For example:

let samplestr = 'Welcome to Itsourcecode';
let [first, second, third] = samplestr.split(" ");
samplestr = `${third}`;  ✅
console.log(samplestr);

Output:

Itsourcecode

Solution 4: Use the slice() method

The slice() method is similar to substring() and extracts a portion of a string between two indices.

For example:

let samplestr = 'Itsourcecode is the best!';
const startIndex = 0; // Start index of the part you want to keep
const endIndex = 12;   // End index of the part you want to keep

const newsampleStr = samplestr.slice(startIndex, endIndex); ✅
console.log(newsampleStr); 

Output:

Itsourcecode

Solution 5: Use the regular expressions:

If you want to remove a specific pattern from a string, you can use regular expressions with replace().

For example:

const samplestr = "Hi, Welcome to Itsourcecode!";
const StringToRemove = /Hi,/; ✅

const newsampleString = samplestr.replace(StringToRemove, "");
console.log(newsampleString);

Output:

 Welcome to Itsourcecode!

Conclusion

We have discussed several ways to remove a part of a string in JavaScript. 

Some of the methods you can use include the split() and pop() methods, the substring() method, template literals, the slice() method, and regular expressions.

It is crucial to understand how each method works. So you can choose the best one for your specific use case.

We hope this article has provided you with enough information to help you understand how to remove part of string JavaScript.

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

Thank you for reading Itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. What is JavaScript string. Read the ‘What is JavaScript string?’ section for the details and code.
  2. Solutions on how to remove part of string in JavaScript. Read the ‘Solutions on how to remove part of string in JavaScript’ section for the details and code.
  3. Conclusion. Read the ‘Conclusion’ section for the details and code.

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