remove first 3 characters from string javascript

How to Remove First 3 Characters from a String in JavaScript

If you have a string in JavaScript and you want to remove the first 3 characters from it, there are several ways to do it. Here are a few:

Method 1: Using the slice() Method

The slice() method returns a portion of a string, starting at the specified index and extending for a given number of characters. To remove the first 3 characters, you can use the slice() method like this:


let str = "Hello World";
let newStr = str.slice(3);
console.log(newStr); // Output: "lo World"

Method 2: Using the substring() Method

The substring() method returns the part of the string between the start and end indexes, or to the end of the string. To remove the first 3 characters, you can use the substring() method like this:


let str = "Hello World";
let newStr = str.substring(3);
console.log(newStr); // Output: "lo World"

Method 3: Using the substr() Method

The substr() method returns a specified part of a string, starting from a specified index and including the specified number of characters. To remove the first 3 characters, you can use the substr() method like this:


let str = "Hello World";
let newStr = str.substr(3);
console.log(newStr); // Output: "lo World"

These are three ways to remove the first 3 characters from a string in JavaScript. Choose the one that works best for your specific use case.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe