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.
Comments (0)