JavaScript strings are immutable, so removing the last character from a string always creates a new string. The original string is not changed. The most common solution is slice(0, -1), but substring(), replace(), and Unicode-aware approaches are useful in specific cases.
This guide shows practical ways to remove the last character from a string in JavaScript and explains when each method is appropriate.
Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).
Method 1: Remove Last Character with slice()
slice(0, -1) is the cleanest way to remove the last character from a JavaScript string.
const text = "golinuxcloud";
const result = text.slice(0, -1);
console.log("slice-last:", result);You should see one line logging slice-last: golinuxclou.
The start index 0 keeps the string from the beginning. The end index -1 stops before the last character.
Method 2: Remove Last Character with substring()
substring() can also create a new string up to length - 1.
const text = "golinuxcloud";
const result = text.substring(0, text.length - 1);
console.log("substring-last:", result);You should see one line logging substring-last: golinuxclou.
Use substring() when you prefer explicit indexes. Use slice() when negative indexes make the code shorter.
Method 3: Remove Last Character with replace()
A regular expression can replace the final character with an empty string.
const text = "golinuxcloud";
const result = text.replace(/.$/, "");
console.log("replace-last:", result);You should see one line logging replace-last: golinuxclou.
The pattern /.$/ matches one character at the end of the string. This is useful when the removal rule is regex-based, but slice() is simpler for normal last-character removal.
Method 4: Remove Last Unicode Character Safely
Some visible characters, such as emoji, can use more than one UTF-16 code unit. For those cases, convert the string with Array.from() first.
const text = "Hi 👋";
const result = Array.from(text).slice(0, -1).join("");
console.log("emoji-safe:", JSON.stringify(result));You should see one line logging emoji-safe: "Hi ".
This removes the visible waving hand character instead of leaving a broken surrogate pair.
Common Questions About Removing the Last Character
What is the best way to remove the last character from a string in JavaScript?
Use str.slice(0, -1) for most strings. It is concise, readable, and does not modify the original string.
Does slice change the original string?
No. JavaScript strings are immutable, so slice() returns a new string.
How do I remove the last character only if it matches something?
Use a condition or a regular expression. For example, str.endsWith("/") ? str.slice(0, -1) : str removes a trailing slash only when it exists.
Summary
To remove the last character from a string in JavaScript, use slice(0, -1) in most cases. substring(0, str.length - 1) is an explicit alternative, replace(/.$/, "") is useful for regex-driven cleanup, and Array.from(str).slice(0, -1).join("") is safer when the last visible character may be an emoji or another Unicode character made from multiple code units.
