KOMPX.COM or COMPMISCELLANEA.COM   

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

  1. String.prototype.substring(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
  2. 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