You can use the substring function:
let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:
let str = "12345.00";
str = str.slice(0, -1);
console.log(str);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Neither method mutates
Answer from Jon Erickson on Stack Overflow Top answer 1 of 16
4055
You can use the substring function:
let str = "12345.00";
str = str.substring(0, str.length - 1);
console.log(str);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This is the accepted answer, but as per the conversations below, the slice syntax is much clearer:
let str = "12345.00";
str = str.slice(0, -1);
console.log(str);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Neither method mutates
2 of 16
1556
You can use slice! You just have to make sure you know how to use it. Positive #s are relative to the beginning, negative numbers are relative to the end.
js>"12345.00".slice(0,-1)
12345.0
Stack Abuse
stackabuse.com โบ bytes โบ trim-the-last-n-characters-from-a-string-in-javascript
Trim the Last N Characters from a String in JavaScript
August 21, 2023 - For instance, you might want to remove the last character from a string only if it is a comma. ... let str = "Hello, World!,"; if (str.endsWith(",")) { str = str.substring(0, str.length - 1); } console.log(str); ... No spam ever. Unsubscribe anytime. Read our Privacy Policy. In the above code, we used the String.endsWith() method to check if the string ends with a comma.
Scaler
scaler.com โบ home โบ topics โบ remove last character from string in javascript
Remove the Last Character from a String in JavaScript - Scaler Topics
May 4, 2023 - Using substring() method substring() method in javascript is used to extract and return a desired part of string and by using this we can remove last character from string javascript. The substring() method takes two parameters in general, the first parameter is the starting index and the second ...
Mastering JS
masteringjs.io โบ tutorials โบ fundamentals โบ remove-last-character
How to Remove the Last Character from a String in JavaScript - Mastering JS
November 12, 2021 - slice() is generally easier, however other methods available are substring() and replace(). substring() does not have negative indexing, so be sure to use str.length - 1 when removing the last character from the string. replace() takes either ...
CoreUI
coreui.io โบ blog โบ how-to-remove-the-last-character-from-a-string-in-javascript
How to Remove the Last Character from a String in JavaScript ยท CoreUI
June 23, 2024 - Removing the last character from a string in JavaScript is a common task, whether youโre dealing with user input or formatting strings. JavaScript provides several methods to achieve this, including slice(), substring(), and replace(). This article will cover these methods in detail, ensuring ...
Linux Hint
linuxhint.com โบ remove-second-last-character-from-string-javascript
Linux Hint โ Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Medium
medium.com โบ @onlinemsr โบ how-to-remove-the-last-character-from-a-string-in-javascript-3d1c238d1669
How to Remove the Last Character from a String in JavaScript | by Raja MSR | Medium
February 15, 2024 - Have you ever wondered how to remove the last character from a string in JavaScript? If you are a web developer, you may have encountered this problem many times, especially when you are working with user input, data processing, or string manipulation. Removing the last character from a string can be tricky, as there are different methods and scenarios to consider. ... By the end of this blog post, you will have a better understanding of how to use JavaScript to remove the last character from a string, and how to apply this skill to various situations.
GeeksforGeeks
geeksforgeeks.org โบ javascript-program-to-remove-last-character-from-the-string
JavaScript Program to Remove Last Character from the String | GeeksforGeeks
June 17, 2024 - These are the following ways to remove first and last characters from a String:1. Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices.
Medium
medium.com โบ @ryan_forrester_ โบ javascript-remove-last-character-from-string-fc1cabdf2722
Javascript Remove Last Character From String | by ryan | Medium
September 10, 2024 - For strings with only one character, removing the last character will result in an empty string. This behavior should be acceptable for most applications, but itโs important to be aware of it. ... Strings in JavaScript are immutable, meaning the original string is not modified by operations like slice() or substring().
Coderslang
learn.coderslang.com โบ 0030-javascript-remove-last-character-from-a-string
JavaScript, remove last character from a string - Coderslang
February 14, 2021 - const base = 'javascript, remove last char!' const s = base.slice(0, -1); console.log(s); // javascript, remove last char ยท The function slice returns a part of the string. We pass in two values, start and end. Eventually slice returns us a substring of base between start and end. If end is a negative number, the lookup begins from the end of the string. ... As the function slice only returns us a substring and doesnโt mutate the original string, we can use the function substring to get the same outcome.