How to Remove the First and Last Character of a String in JavaScript

Hero image for How to remove the first and the last character of a string

Learn various efficient methods to trim the first and last characters from a string in JavaScript, covering common use cases and best practices.

Removing the first and last characters of a string is a common operation in JavaScript, often needed for data cleaning, formatting, or parsing. Whether you're dealing with unwanted delimiters, quotation marks, or simply need to extract a substring, JavaScript provides several flexible methods to achieve this. This article will explore the most common and efficient techniques, along with their practical applications.

Understanding String Immutability

Before diving into the methods, it's crucial to understand that strings in JavaScript are immutable. This means that string methods do not modify the original string; instead, they return a new string with the desired changes. This is an important concept to remember when working with string manipulation.

flowchart TD
    A[Original String] --> B{String Method (e.g., slice())}
    B --> C[New String (Modified)]
    C --X A
    A -- "Remains Unchanged" --> A

String Immutability in JavaScript

Method 1: Using slice()

The slice() method is arguably the most straightforward and commonly used approach for extracting a portion of a string. It takes two optional arguments: a startIndex and an endIndex. When removing the first and last characters, you can specify 1 as the startIndex (to skip the first character) and -1 as the endIndex (to exclude the last character).

const originalString = "Hello World!";
const trimmedString = originalString.slice(1, -1);

console.log(trimmedString); // Output: "ello World"

Using slice(1, -1) to remove first and last characters.

Method 2: Using substring()

Similar to slice(), the substring() method also extracts characters between two indices. However, substring() handles negative indices differently. If a negative index is provided, substring() treats it as 0. To remove the first and last characters, you'd use 1 as the startIndex and string.length - 1 as the endIndex.

const originalString = "[Data]";
const trimmedString = originalString.substring(1, originalString.length - 1);

console.log(trimmedString); // Output: "Data"

Using substring(1, string.length - 1) for trimming.

Method 3: Using substr() (Legacy)

The substr() method is a legacy function that is generally discouraged in new code due to its non-standard behavior and eventual deprecation. It takes a startIndex and a length argument. To remove the first and last characters, you'd start at index 1 and specify a length of string.length - 2.

const originalString = """Quoted Text""";
const trimmedString = originalString.substr(1, originalString.length - 2);

console.log(trimmedString); // Output: "Quoted Text"

Using substr(1, string.length - 2) (legacy method).

Choosing the Right Method

For most scenarios, slice() is the preferred method due to its intuitive handling of negative indices and its conciseness. substring() is a good alternative if you prefer explicit positive indices, but remember its behavior with negative inputs. substr() should be avoided in new development.

Hero image for How to remove the first and the last character of a string

Comparison of string trimming methods.