Remove last character from string JavaScript
In this post, you will learn how to remove the last character from string using the JavaScript programming language.
This is a very common JavaScript interview question and is generally asked in programming interviews, 'how to remove the last character from a string in JavaScript'. Or, during the development process, you may come across a situation where you need to eliminate the last character from a string. This exercise shows you different ways to achieve the process from the given string. You can use any of the following methods to remove the last character from the string.
Remove last character from string using substring() method
The string.substring() is an inbuilt function in JavaScript which is used to return the part of the given string from the start index to the end index.
Syntax-string.substring(startindex, endindex)
Here the startindex and endindex describes the part of the string to be taken as substring. Here the endindex is optional.
Example-// initialize variable
var str = "Welcome etutorialspoint";
// remove last character of string
str = str.substring(0, str.length - 1);
// print result string
console.log(str)
Output of the above code:
Welcome etutorialspoin
Remove last character from string using slice() method
The JavaScript array slice() method extracts the part of the given array and returns it. This method doesn't change the original array.
Syntax-array.slice(start,end)
Here, start and end are optional. The start represents the index from where the method starts to extract the elements and end represents the index at where the method stops extracting elements.
Example-// initialize variable
var str = "Welcome etutorialspoint";
// remove last character of string
str = str.slice(0, -1);
// print result string
console.log(str)
Output of the above code:
Welcome etutorialspoin
Remove last character from string using substr() method
The str.substr() method of JavaScript returns the specified number of characters from the specified index from the given string.
Syntax-string.substr(start, length)
Here, the start defines the starting index from where the substring is to be extracted from the base string and the length defines the number of characters to be extracted starting from the start in the given string.
Example-// initialize variable
var str = "javascript tutorial";
// remove last character of string
str = str.substr(0, str.length - 1);
// print result string
console.log(str)
Output of the above code:
javascript tutoria
Related Articles
Generate random numbers in JavaScriptJavascript window location
Remove duplicates from array Javascript
Scientific calculator JavaScript
How to reverse string in JavaScript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh