JavaScript

How to Remove Second Last Character From String in JavaScript

While programming in JavaScript, we come across situations involving the use of complex string arrays comprising some punctuation or grammar mistakes. For instance, when you test a web page or a website and face some issues in the DOM section particularly. In such cases, removing the second last character from a string in JavaScript can save your time and effort.

This manual will guide you to remove the second last character from the string in JavaScript.

How to Remove Second Last Character From String in JavaScript?

To remove second last character from a string in JavaScript, use:

  • slice()” and “addEventListener()” methods
  • substring()” method

We will now go through each of the two approaches one by one!

Method 1: Remove Second Last Character From String in JavaScript Using slice() and addEventListener() Methods

The “addEventListener()” method attaches an event handler to a document, while the “slice()” method returns selected elements as a new array. These methods can be used in combination to add a click event and slice the given string when the button is clicked.

Syntax

string.slice(start, end)

In the given syntax, “start”, and “end” refers to the start and end indexes of the given string, respectively.

document.addEventListener(click, function)

In the above syntax, “click” refers to the specified event, and “function” is the function on which the event is to be applied.

The following example explains the stated concept clearly.

Example
Firstly, create a button and a heading that will be displayed on the Document Object Model(DOM) as follows:

<button>Click Me</button>
<h2>Output</h2>

Now, access the created button and heading using the “document.querySelector()” method:

let button= document.querySelector("button");
let output= document.querySelector("h2");

After that, specify a string value named “Character” and log it on the console:

let string= "Character";  
console.log("The given string value is:", string)

Finally, add a “click” event to the created button. Also, apply the “slice()” method along with the “length” property in such a way that the second last character is extracted out.

In the below demonstration, the slice() method will first refer to the whole string with the last two values subtracted, and then the second slice() method will only extract the last string character. Both of the fetched substrings will be fetched and concatenated using the “+” operator and displayed as the innerHTML of the heading refers as the “output”:

button.addEventListener("click", () => {
  let secondLast = string.slice(0, string.length - 2) +  string.slice(string.length - 1);
   output.innerText = secondLast;
});

Output

Method 2: Remove Second Last Character From String in JavaScript Using substring() Method

The “substring()” method extracts characters from start to end without any change in the original string. This method can be implemented to locate the second last character from the given string by separating the last two characters and merging the last character in the string only.

Syntax

string.substring(start, end)

In this particular syntax, “start” and “end” refer to the start and end indexes, respectively, similar to the slice method.

Example
First of all, we will store a string value named “JavaScript” and log it on the console:

const string= 'JavaScript';                      
console.log("The given string value is:", string)

Now, apply the “substring()” method by referring to the string indexes in such a way that the second last string character is located and omitted using the same approach which is discussed in the previous method:

const secondLast= string.substring(0, string.length - 2) + string.substring(string.length - 1);

Finally, display the resultant string value on the console:

console.log("The resultant string after removing second last character is: ", secondLast);

Output

We have discussed all the easiest methods to remove the second last character from string in JavaScript.

Conclusion

To remove the second last character from a string in JavaScript, use the “slice()” and “addEventListener()” methods for adding a click event and extracting the second last character upon the button click or the “substring()” method for separating the last two characters and merging the last character in the string only. This blog explained the method to remove the second last character from a string in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.