Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu February 22, 2023 › #HowTo #Javascript

Remove last 2 characters from string using JavaScript

Today I'm going to show you how to remove the last two characters from a string using JavaScript.

The first step is to create a variable that holds our string. The example is given below.

<script>
  let myString = "This is a sample string";
</script>

Next, we can use the slice() method in order to extract part of our string and assign it into a new variable.

<script>
  let myString = "This is a sample string";
  let newString = myString.slice(0,-2);// Removes last 2 characters
  console.log(newString)
</script>

Conclusion #

The above example will remove the last two characters from the variable and assign them into a new variable. If you need more information on other ways of manipulating strings in JavaScript then be sure check out the below tutorials as well.

save
listen
AI Answer
Write Your Answer
loading
back
View All