JavaScript

Remove the last character of a string in Javascript

In this tutorial, we are going to see two methods to remove the last character from a string in the JavaScript programming language. You can use any of the following methods as needed.
 

Method 1 – Using substring function

substring() function returns the part of the string between the start and end indexes.
 
Syntax:

str.substring(0, str.length - 1);

 
Example:

var str = "StackHowTo"; 
var newStr = str.substring(0, str.length - 1);
console.log(newStr);

Output:

StackHowT
 

Method 2 – Using slice function

This function extracts part of a string and returns the new string.
 
Syntax:

str.slice(0, -1);

 
Example:

var str = "StackHowTo"; 
var newStr = str.slice(0, -1);
console.log(newStr);

Output:

StackHowT
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *