Removing the last character from a string in Javascript
Trimming the last character in a string of "example" in Javascript:
HTML code:
<div class="example"></div>
JavaScript code:
<script>
var exampleString = "example";
exampleString = exampleString.substring(0, exampleString.length - 1);
// Printing the result
document.querySelector('.example').textContent = exampleString;
</script>
Links
- String.prototype.substring(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
- JavaScript String substring(): w3schools.com/jsref/jsref_substring.asp
Browser support
- Windows
- Internet Explorer 4.0+
- Edge 12.0+
- Firefox 1.0+
- Google Chrome 1.0+
- Opera 3.0+
- Safari 3.1+
- SeaMonkey 1.0+
- Mozilla 0.6+
- Netscape 2.02+
- Linux
- Firefox 1.0+
- Google Chrome / Chromium
- Opera 5.0+
- SeaMonkey 1.0+
- Mozilla 0.6+
- Netscape 2.02+
- Hv3
- iOS
- Safari 1.0+
- Android
- Samsung Internet 1.0+
- Chrome 18.0+
- Firefox 4.0+
- Opera 10.1+
More
- Adding a class to all sibling elements of a specific type in JavaScript
- Array to string in JavaScript
- Calculate sum of HTML table column in JavaScript
- Detecting a newly generated element and adding a class to it in JavaScript
- Detect if a class has been added (JavaScript)
- Getting the last class name in classList collection
- Transliterating Russian to English in JavaScript