Languages
[Edit]
EN

JavaScript - remove last 3 characters from string

3 points
Created by:
Adnaan-Robin
724

In this article, we're going to have a look at how to remove the last 3 characters from the string in JavaScript.

1. String slice() method example

Edit

This approach allows getting substring by using negative indexes. So by using 0 and -3 indexes as the range we get <0, text.length - 3> text range. 

2. String substring() method example

Edit

This is an alternative approach to slice method based solution.

3. String replace() method example

Edit

There is another trick that allows replacing all characters that match expression condition. By using .{0,3}$ pattern we match any 3 characters (up to 3 characters) that are located at the end of the string.

Alternative titles

  1. JavaScript - how to chop off last 3 chars from string?
  2. How do I trim off last 3 characters in JavaScript?
  3. JavaScript - delete last 3 chars of string
  4. Slice off last 3 characters in js
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - String (popular problems)

JavaScript - remove last 3 characters from string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join