Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

JavaScript - remove last 3 characters from string

0 contributions
3 points

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

1. String slice() method example

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

// ONLINE-RUNNER:browser;

var text = 'abcde';
var substring = text.slice(0, -3);

console.log(substring); // ab

2. String substring() method example

This is alternative approach to slice method based solution.

// ONLINE-RUNNER:browser;

var text = 'abcde';
var substring = text.substring(0, text.length - 3);

console.log(substring); // ab

3. String replace() method example

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

// ONLINE-RUNNER:browser;

var text = 'abcde';
var substring = text.replace(/.{0,3}$/, '');

console.log(substring); // ab
0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ đź’» 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more