Table of contents
1.
Introduction
2.
Approach 1: Using for Loop
3.
Approach 2: Using the slice() Method
4.
Approach 3: Using the substring() Method
5.
Approach 4: Using split() and join() Method
6.
Approach 5: Using Regular Expression
7.
Approach 6: Using Array.from() Method
8.
Approach 7:  Using the `replace()` Method  
8.1.
When to Use `replace()`:
9.
Frequently Asked Questions
9.1.
Which method is the most efficient to remove the last character from the string in JavaScript?
9.2.
Can these methods handle empty strings? 
9.3.
Are these methods applicable to all JavaScript environments? 
10.
Conclusion
Last Updated: Jan 26, 2025
Medium

Remove Last Character from String in JavaScript

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Removing the last character from a string in JavaScript is a common task often required during data manipulation or formatting. JavaScript provides several methods to achieve this, such as using the slice() or substring() functions. These techniques are versatile and enable developers to handle strings efficiently without altering the original data directly.

In this article, we will learn how to remove the last character from a string in JavaScript using simple and easy-to-understand methods.

Approach 1: Using for Loop

Using a for loop is a basic way to manipulate a string. By iterating through the string, we can construct a new string without including the last character.

function removeLastCharUsingLoop(str) {
    let result = ""; // Initialize an empty string
    for (let i = 0; i < str.length - 1; i++) {
        result += str[i]; // Append all characters except the last one
    }
    return result;
}
// Example
const input = "Hello!";
console.log(removeLastCharUsingLoop(input)); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

"Hello"


Explanation:

  • The loop runs from the first character to the second-to-last character.
     
  • Each character is added to the result string, which excludes the last character.

Approach 2: Using the slice() Method

The slice() method is a simple and effective way to remove the last character from a string.

function removeLastCharUsingSlice(str) {
    return str.slice(0, -1); // Extracts all but the last character
}
// Example
const input = "JavaScript";
console.log(removeLastCharUsingSlice(input)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"JavaScrip"


Explanation:

  • slice(0, -1) means start from the beginning (0) and exclude the last character (-1).
     
  • This method is concise and easy to use.

Approach 3: Using the substring() Method

The substring() method can also remove the last character by specifying the start and end indices.

function removeLastCharUsingSubstring(str) {
    return str.substring(0, str.length - 1); // Extract substring excluding the last character
}
// Example
const input = "Developer";
console.log(removeLastCharUsingSubstring(input)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"Develope"


Explanation:

  • substring(0, str.length - 1) takes characters from index 0 to length - 1.
     
  • It’s similar to slice() but with slight differences in behavior for negative indices (not applicable here).

Approach 4: Using split() and join() Method

This approach splits the string into an array, removes the last element, and joins the array back into a string.

function removeLastCharUsingSplitJoin(str) {
    let arr = str.split(""); // Convert string to an array
    arr.pop(); // Remove the last element
    return arr.join(""); // Join the array back to a string
}
// Example
const input = "Frontend";
console.log(removeLastCharUsingSplitJoin(input)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"Fronte"


Explanation:

  • split("") turns the string into an array of characters.
     
  • pop() removes the last element of the array.
     
  • join("") combines the array back into a string.

Approach 5: Using Regular Expression

Regular expressions (regex) provide a powerful way to modify strings, including removing the last character.

function removeLastCharUsingRegex(str) {
    return str.replace(/.$/, ""); // Replace the last character with an empty string
}
// Example
const input = "Backend";
console.log(removeLastCharUsingRegex(input)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"Backen"


Explanation:

  • The regex /$/ matches the last character of the string.
     
  • The replace() method replaces it with an empty string.

Approach 6: Using Array.from() Method

Array.from() converts a string into an array-like structure, making it easy to manipulate characters.

function removeLastCharUsingArrayFrom(str) {
    let arr = Array.from(str); // Create an array from the string
    arr.pop(); // Remove the last element
    return arr.join(""); // Join the array back to form a string
}
// Example
const input = "FullStack";
console.log(removeLastCharUsingArrayFrom(input)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"FullStac"


Explanation:

  • Array.from() creates an array from the string.
     
  • The rest of the logic is similar to split() and join().

Approach 7:  Using the `replace()` Method  

The `replace()` method is a built-in JavaScript function that allows you to replace a specific part of a string with another value. It is commonly used for replacing substrings, but it can also be used to remove the last character of a string.  

To remove the last character, we can use a regular expression (regex) to target the last character & replace it with an empty string. Let’s see how it works:  

let str = "Hello!";
let newStr = str.replace(/.$/, "");
console.log(newStr); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

Hello


In this Code:  

1. `str.replace(/.$/, "")`:  

  • The `replace()` method takes two arguments: the pattern to search for & the replacement value.  
     
  • The regex `/.$/` is used to match the last character of the string.  

    - `.` matches any single character.  

    - `$` ensures that the match is at the end of the string.  
     
  • The second argument is an empty string `""`, which replaces the matched character with nothing, effectively removing it.  


2. Example :  

  • Let’s say the string is `"Hello!"`.  
     
  • The regex `/.$/` matches the last character `!`.  
     
  • The `replace()` method removes `!` & returns `"Hello"`.  

When to Use `replace()`:

  • Use this method when you need to remove a specific character or pattern from a string.  
     
  • It is especially useful when you know the exact pattern of the character you want to remove.  

Frequently Asked Questions

Which method is the most efficient to remove the last character from the string in JavaScript?

The slice() method is often the most efficient and readable option for this task.

Can these methods handle empty strings? 

Yes, but using methods like slice() or substring() on an empty string will return an empty string. Ensure to handle such cases explicitly if required.

Are these methods applicable to all JavaScript environments? 

Yes, all the methods discussed are standard JavaScript features and are supported in most environments.

Conclusion

In this article, we discussed six methods to remove the last character from a string in JavaScript, including for loops, slice(), substring(), split() with join(), regex, and Array.from(). Each method has its advantages, and the choice depends on your specific use case. 

Live masterclass