How can I remove the last character from a string in JavaScript?

I have a string like “12345.00” and I want to trim off the last character so that it becomes “12345.0”.

I checked out trim(), but that only removes whitespace. Is there a proper method in JavaScript to remove last character from string?

You can use the slice() method like this:

let str = "12345.00";
let trimmed = str.slice(0, -1); // "12345.0"

slice(0, -1) returns a new string without the last character. It’s the most straightforward way.

If you prefer using substring method:

let str = "12345.00";
let trimmed = str.substring(0, str.length - 1);

Same result, but substring method might feel more explicit if you’re newer to negative indexing.

If you want to remove the last character only if it’s a zero:

let str = "12345.00";
if (str.endsWith("0")) {
  str = str.slice(0, -1);
}