How To Remove The Last 3 Characters From A String Using JavaScript

How To Remove the Last 3 Characters from a String using JavaScript

To remove the last 3 characters from a string using JavaScript, we have three methods: using the splice() method, using the substring() method, or using the replace() method. Check out the details below to get more information.

Remove the last 3 characters from a string using JavaScript

Using the slice() method

Removing the last 3 characters from a string using JavaScript is standard programming that programmers have to do. To remove the last three characters from a string in Javascript, we can easily use the slice() method. It is already built-in function in Javascript.

Syntax:

array.slice(start, end)

Parameters: 

  • start: start position. The default is 0.
  • end: end position. Default is the last element.

Return value: the return value is a new array containing the selected elements.

Code example: 

In this example, we will create a newString to get the value of the myString after using the splice() method to remove the last 3 elements.

Example:

var myString = 'LearnShareIT123';
console.log("myString: ",myString);

//we can use the slice method by using 0 and -3 indexes 
var newString = myString.slice(0, -3);
 
console.log("After removing the last 3 elements in MyString: ",newString);

Output

myString: LearnShareIT123
After removing the last 3 elements in MyString: LearnShareIT

Using the substring() method

The other simple way to remove the last 3 characters from a string is to use the subString() method. 

Syntax:

array.substring(start, end)

Parameters: 

  • start: start position. The first character is at index 0.
  • end: end position (up to, but not including).

Return value: the return value is a string containing the extracted characters.

Example:

var myString = 'LearnShareIT123';
console.log("myString: ",myString);

//we can use the substring() method to remove the last 3 characters of myString 
var newString = myString.substring(0, myString.length - 3);
 
console.log("After removing the last 3 elements in MyString: ",newString);

Output

myString: LearnShareIT123
After removing the last 3 elements in MyString: LearnShareIT

Using the replace() method

There is another method that allows replacing all characters that match the expression condition.

Syntax:

string.replace(searchValue, newValue)

Parameters: 

  • searchValue: The value, or regular expression, to search for.
  • newValue: The new value.

Return value: the return value is a string where the specified values have been replaced.

Example:

var myString = 'LearnShareIT123';
console.log("myString: ",myString);

//we use the regular expression to get the last 3 characters and replace with ''
var newString = myString.replace(/.{0,3}$/, '');
 
console.log("After removing the last 3 elements in MyString: ",newString);

Output

myString: LearnShareIT123
After removing the last 3 elements in MyString: LearnShareIT

Summary

In this tutorial, we have explained how to remove the last 3 characters from a string using JavaScript. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *